我正在使用扫描仪读取文件。该文件被格式化,第一行是数组的维度。下一行包含第一行,下一行是第二行,依此类推。示例文件:
3 3 1 2 3 4 5 6 7 8 9
我一直遇到的问题是我的数组值似乎都是 9。文件是整数,但我需要双精度数。当我试图调试正在发生的事情时,我也有很多打印语句。我的异常处理还没有完成。在我可以正确地实例化数组之后,我会回去加强它。任何指针将不胜感激。例如:只需打开文件一次即可获取维度和实例化数组的更好方法。
更新但得到一个 nullPointerException
public class Help implements TopoMapInterface {
private String filename;
private File mapfile;
public double[][] baseMap;
public double[][] enMap;
public int enhancementLevel;
public Help(String filename) throws FileNotFoundException,
InvalidFileFormatException {
this.filename = filename;
System.out.println("Reading in file: " + filename);
String number = "";
int row = 0;
int col = 0;
int count = 0;
try {
Scanner inputFile = new Scanner(new File(filename));
while (inputFile.hasNextInt()) {
row = Integer.parseInt(inputFile.next());
col = Integer.parseInt(inputFile.next());
System.out.println("Row : " + row);
System.out.println("Col : " + col);
baseMap = new double[row][col];
System.out.println(baseMap[2][4]);
for (int i = 0; i < baseMap.length; i++){
for (int j = 0; j < baseMap[i].length; j++){
baseMap[i][j] = Double.parseDouble(inputFile.next());
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
}