繁体   English   中英

如何从文件中读取整数(X和Y点)并在Java中绘制最小回归线? 错误

[英]How to read in integers (X and Y points) from a file and plot a least regression line in Java? Error

因此,假设我要从.txt文件中读取一组点,并在这些点的顶部绘制最小二乘回归线。 (我也不熟悉统计信息)

我的程序将使用文本绘制点(例如,“ X”代表点,“-”代表回归线段,“ *”代表线段和点位于同一点)

这些是要从文件读取的点(第一个整数x和第二个y)。 x坐标在[0,40]范围内,y坐标在[1,20]范围内。

20 10
0  1
40 20
13 17
13 12
10 ?
the x coordinates represent time
15 0
10 20

There are some invalid points in this text but the program would eventually ignore it but do not stop reading the file.

这是我想出的,但仍然给出ArrayIndexBound:2的错误

import java.awt.*;
import java.io.*;
import java.util.StringTokenizer;

public class A{
    public static void main(String[] args){

        processValuesFromFile("points.txt");

    }

    public static void processValuesFromFile(String filename){
        BufferedReader inputFile;
        String line;
        int[][] points;

        try{
            inputFile = new BufferedReader(new FileReader(filename));
            line = inputFile.readLine();

            while(line!= null){
                points = readPoints(inputFile,line);
                printPoints(points);
                //
                line = inputFile.readLine();
            }
            inputFile.close();



        }catch (IOException | ArrayIndexOutOfBoundsException ioe){
            System.out.println(ioe.getMessage());
            //ioe.printStackTrace();
        }
    }

    public static int[][] readPoints(BufferedReader inputFile, String line) throws IOException{
        String[] split;
        String inputLine;
        int[][] points;
        int pointSize = 9;

        points = new int[pointSize][pointSize];
        for(int row=0; row<pointSize;row++){
            inputLine = inputFile.readLine();
            System.out.println(inputFile);
            split = inputLine.split("\\s+");

            for(int col =0; col <pointSize;col++){
                points[row][col] = Integer.parseInt(split[col]);
            }

        }
        return points;

    }

    public static void printPoints(int[][] points) {
        for (int row=0; row < points.length; row++) {
            for (int col=0; col < points[row].length; col++) {
                System.out.print(points[row][col] +"\t");
            }
            System.out.println();
        }
    }
}

最好创建一个代表要点的类。

class Point{

int x;
int y;

}

然后创建一个数组或点的集合

Point[] points = new Point[10];

要么

List<Point> points = new ArrayList<Point>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM