【发布时间】:2014-10-26 20:14:28
【问题描述】:
错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "7 1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:19)
我正在尝试按如下方式读取文件内容,就像出现逗号一样,表示图中有一个直接箭头!从 3 到 7 有一个直接箭头,从 1 到 4 等等。
file.txt 内容:
3,7 1,4 7,8 0,5 5,2 3,0 2,9 0,6 4,9 2,6 6,4 1,5 8,2 9,0 8,3 4,5 2,3 1,6 3,5 7,6
java代码:
导入 java.io.File; 导入 java.util.Scanner;
公共类 JavaApplication3 {
public static void main(String[] args) throws Exception
{
@SuppressWarnings("resource")
Scanner inFile = new Scanner(new File("file.txt"));
// Read the number of vertices
String line = inFile.nextLine();
String[] data=line.split("\\,");
int part1=Integer.parseInt(data[0]);
int part2=Integer.parseInt(data[1]);
\*I expect the 2 lines above to work like that :part1=3, part2=7 .but what I noticed there is aproblem about the space character .*/
while(inFile.hasNext())
{
line=inFile.nextLine();
int index =0;
int count=0;
char edges = ',';
while(index<line.length()) {
if(line.charAt(index)==edges){
count++;
}
index++;
}
}
}
.
如果可以的话请帮帮我。
【问题讨论】: