[英]Read text file in java eclipse as 2d array error
我有这段代码从文本文件中读取数据并将其分配给2d数组
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")
希望这些解决方案之一为您服务!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.