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