Java 将填充有双精度字符的文本文件读取到二维数组中
免责声明:这是家庭作业,所以我不寻找确切的答案,因为我想自己做这件事。 我必须从一个文本文件中读取一个矩阵,该文件中填充了双精度字符,并放入一个2D数组中。 我目前正努力找出代码中的问题所在,因为我知道如何将内置Java扫描程序用于普通数组。下面的代码总是给我一个NullPointerException错误,这意味着我的矩阵2D数组是空的Java 将填充有双精度字符的文本文件读取到二维数组中,java,arrays,Java,Arrays,免责声明:这是家庭作业,所以我不寻找确切的答案,因为我想自己做这件事。 我必须从一个文本文件中读取一个矩阵,该文件中填充了双精度字符,并放入一个2D数组中。 我目前正努力找出代码中的问题所在,因为我知道如何将内置Java扫描程序用于普通数组。下面的代码总是给我一个NullPointerException错误,这意味着我的矩阵2D数组是空的 public Matrix(String fileName){ //reads first double and puts it into the [
public Matrix(String fileName){
//reads first double and puts it into the [1][1] category
//then moves on to the [1][2] and so on
//once it gets to the end, it switches to [2][1]
Scanner input = new Scanner(fileName);
int rows = 0;
int columns = 0;
while(input.hasNextLine()){
++rows;
Scanner colReader = new Scanner(input.nextLine());
while(colReader.hasNextDouble()){
++columns;
}
}
double[][]matrix = new double[rows][columns];
input.close();
input = new Scanner(fileName);
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
if(input.hasNextDouble()){
matrix[i][j] = input.nextDouble();
}
}
}
}
您的代码有几个错误-由于这是家庭作业,我将尽可能坚持您的原始设计:
public Matrix(String fileName){
//First issue - you are opening a scanner to a file, and not its name.
Scanner input = new Scanner(new File(fileName));
int rows = 0;
int columns = 0;
while(input.hasNextLine()){
++rows;
columns = 0; //Otherwise the columns will be a product of rows*columns
Scanner colReader = new Scanner(input.nextLine());
while(colReader.hasNextDouble()){
//You must "read" the doubles to move on in the scanner.
colReader.nextDouble();
++columns;
}
//Close the resource you opened!
colReader.close();
}
double[][]matrix = new double[rows][columns];
input.close();
//Again, scanner of a file, not the filename. If you prefer, it's
//even better to declare the file once in the beginning.
input = new Scanner(new File(fileName));
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
if(input.hasNextDouble()){
matrix[i][j] = input.nextDouble();
}
}
}
}
请注意注释-总体而言,您离功能代码不远,但理解错误很重要。NullPointerException发生在哪里?查看堆栈跟踪,看看是否可以确定异常发生的位置。@Ravi这个问题非常笼统且过于宽泛。我不鼓励将此问题作为该问题的重复问题来结束。@DefenseTrator错误发生在我的toString函数中,这是我的嵌套for循环中第一个看到2d数组长度的函数。@azurefrog忘记将其更改回hasNextDouble,我只是在测试。@Michael在您发布的代码中没有对toString的调用。您发布的代码是否再现了错误?
public Matrix(String fileName){
//First issue - you are opening a scanner to a file, and not its name.
Scanner input = new Scanner(new File(fileName));
int rows = 0;
int columns = 0;
while(input.hasNextLine()){
++rows;
columns = 0; //Otherwise the columns will be a product of rows*columns
Scanner colReader = new Scanner(input.nextLine());
while(colReader.hasNextDouble()){
//You must "read" the doubles to move on in the scanner.
colReader.nextDouble();
++columns;
}
//Close the resource you opened!
colReader.close();
}
double[][]matrix = new double[rows][columns];
input.close();
//Again, scanner of a file, not the filename. If you prefer, it's
//even better to declare the file once in the beginning.
input = new Scanner(new File(fileName));
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
if(input.hasNextDouble()){
matrix[i][j] = input.nextDouble();
}
}
}
}