因此,假设我要从 .txt 文件中读取一组点并在这些点的顶部绘制最小二乘回归线。 (我对统计数据也不太熟悉)
My program would plot the points using text (for example, with "X"s representing the points, "-"s the regression line segments, and " * "s where a line segment and a point are located at the same spot)
这些是要从文件中读取的点(第一个整数 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
此文本中有一些无效点,但程序最终会忽略它,但不会停止读取文件。
这是我想出的,但仍然给出 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>();
关于java - 如何从文件中读取整数(X 和 Y 点)并在 Java 中绘制最小回归线?错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511844/