0
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)
我试图读取文件contet如下,就像如果逗号出现,这意味着有直接的箭头在图中!从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; import 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++;
}
}
}
。
帮助我,如果你可以感谢你。
我会建议使用正则表达式““[\\秒]”'所以它会在这样的类空字符分割作为标签。 – engineercoding 2014-10-26 20:44:59
@codequestioneer是的,如果该文件可能使用其他空白字符。我想它可能不会在这里,但我会编辑我的答案,以防万一。它看起来在某些地方连续有多个空格。感谢您的建议。 – 2014-10-26 21:00:35
我试过“[,\\ s]”,但也有问题,所以我尝试了第二个选项String [] data = line.split(“,| \\ s +”);现在它可以工作,也许是因为我使用了多个空格,谢谢你@Lonenebula – nana 2014-10-27 13:55:42