只是想知道我在这里做错了什么。我正在尝试从文件中读取并解析一些数据。我不断收到错误“java.lang.NumberFormatException”,它无法解析数字。我知道我使用 getNextDouble() 或获取 nextInt() 但我尽量保持代码通用。任何想法将不胜感激。泰。
这是我的文本文件中的一行示例:
foo Bar 19.35 55 987.0054 4
public void readFile(){
int i=0;
String line;
try{
fileIn = new Scanner(new File("pa1Names.txt"));
}catch(Exception e){
System.out.println("Fatal Error: File was not opened");
}
while(i<names.length && fileIn.hasNext()){
line = fileIn.nextLine();
StringTokenizer st = new StringTokenizer(line);
int m = st.countTokens();
names[i] = st.nextToken();
for (int k = 0; k<m-5; k++)
{
names[i] = names[i] + " " + st.nextToken();
}
reals[i][0] = Double.parseDouble(st.nextToken());
ints[i][0] = Integer.parseInt(st.nextToken());
reals[i][1] = Double.parseDouble(st.nextToken());
ints[i][1] = Integer.parseInt(st.nextToken());
i++;
}//end while
fileIn.close();
}