我有这段代码从文本文件读取数据并将其分配给二维数组
Scanner read_blockage = null;
General_Inputs.Blockage_Number=new double[Input.General_Inputs.Num_Of_Analysis_Years*Input.General_Inputs.Num_Of_States][Input.General_Inputs.Num_Of_Ppes];
try{
read_blockage=new Scanner(new File("Blockage Output1"));
int row = -1; // since we're incrementing row at the start of the loop
while(read_blockage.hasNext()) {
row++;
String[] line = read_blockage.nextLine().split("\t");
for(int j=0;j<Input.General_Inputs.Num_Of_Ppes;j++){
try {
General_Inputs.Blockage_Number[row][j] = Double.parseDouble(line[j]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}}
read_blockage.close();}catch (FileNotFoundException e) {
e.printStackTrace();
}
但我收到此错误:
java.io.FileNotFoundException: Blockage Output1 (The system cannot find the file specified)
2
2
2
3
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at Input.Get_Inputs(Input.java:270)
at Input.main(Input.java:288)
我不确定为什么会出现此错误,有什么建议吗?
编辑: 我摆脱了上述错误,但现在出现了一个新错误:
java.lang.NumberFormatException: For input string: "0.2810496821150867 0.3455471819235053 0.1247760656600859 0.1925735036025203 0.16475561749067555 0.3267969645821732 0.5325079154577266 0.7311354592633524 0.29828747755582985 0.3983939064000447 1.6432540332118697 2.242416989842468 0.8199042126197025 1.1448149650482649 0.6569387483611318 0.35521248909704994 0.8311372587904973 1.2599707232227086 1.4153816162469934 1.091443886313361 0.7492391207620115 1.4029328027711394 1.3060173850919903 3.0212129386585675 1.185220575726193 3.2093022651230037 2.2304670167490195 4.028061408800144 1.1957020911741867 2.3250822033050813 6.144104904071859 9.634733755857885 3.3148373093880736 9.740483573762857 3.857137427951027 4.527035922001198 7.248709304936811 10.112180962036412 12.688211002013142 3.5445943135631026 5.87022858087266 11.490999298946353 13.75534054772614"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Input.Get_Inputs(Input.java:277)
at Input.main(Input.java:288)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Input.Get_Inputs(Input.java:277)
at Input.main(Input.java:288)
有什么推荐吗?
最佳答案
该错误消息非常有用,它表示程序无法在您使用文件声明指向的任何位置找到该文件。
尝试使用文件“Blockage Output1”的绝对路径,并记住包含文件扩展名(.txt、.conf、.bla)。
//correcting the escape sequence usage
new File("C:\\workspace\\project\\Blockage Output1.txt")
Eclipse 将在项目的根目录中查找相对定义的文件,因此如果您的文件位于 src、bin、res 等文件夹中,那么您需要像这样声明该文件。
//correcting the escape sequence usage
new File("src\\Blockage Output1.txt")
希望这些解决方案之一适合您!
关于java - 在java eclipse中读取文本文件作为二维数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215490/