2014-02-16 68 views
0

當我試圖將String的最後部分解析爲整數時,我得到NumberFormatException。例外打印如下:使用Integer.parseInt(String arg)時發生Java NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "95 
" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at GradeBook.parseDataToStudentArray(GradeBook.java:85) 
    at GradeBook.main(GradeBook.java:12) 

我正在運行一個for循環將一個長字符串分解成多個部分,然後從這些部分創建對象。這裏是方法:

private static Student[] parseDataToStudentArray(String data) 
    { 
     Student[] students = new Student[10]; 
     System.out.print(data); 
     for (int i=0;i<10;i++) 
     { 
      String tempStudent = data.substring(0,data.indexOf("\n")); 
      data=data.substring(data.indexOf("\n")+1); 
      String firstName= tempStudent.substring(0,tempStudent.indexOf(" ")); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      String lastName= tempStudent.substring(0,tempStudent.indexOf(" ")); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      int hw= Integer.parseInt(tempStudent.substring(0,tempStudent.indexOf(" "))); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      int quiz= Integer.parseInt(tempStudent.substring(0,tempStudent.indexOf(" "))); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      int project= Integer.parseInt(tempStudent.substring(0,tempStudent.indexOf(" "))); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      int midterm= Integer.parseInt(tempStudent.substring(0,tempStudent.indexOf(" "))); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      int finalExam= Integer.parseInt(tempStudent); 
      tempStudent = tempStudent.substring(tempStudent.indexOf(" ")+1); 
      students[i] = new Student(firstName,lastName,hw,quiz,project,midterm,finalExam); 
     } 
     return students; 
    } 

非常感謝你的幫助!

我開始與我是否是System.out.print (數據) 產生

John Smith 92 80 86 76 95 
Mary Lamb 66 89 92 100 56 
Katy Perry 80 75 89 83 90 
Mile Johnson 90 92 95 91 88 
Jefferson Hue 75 78 70 82 73 
Gabby Alden 83 79 88 94 92 
Rubby Barry 89 82 75 90 86 
Brian Wilson 78 83 81 89 90 
Davis Brown 92 78 50 77 84 
Alfred Williams 87 93 67 82 95 
+0

是否有一些新興的學生管理行業,或者現在的計算機課程創作者只是懶惰地思考更具創造性的任務?啊,順便說一句,只是說出「出錯」的問題描述不會幫助我們幫助你。 – qqilihq

+0

當你使用你的調試器時,它告訴你有關傳遞給Integer.parseInt的值會給你帶來麻煩嗎? –

+2

你從未聽說過「split」嗎? –

回答

3

我強烈建議你使用String#split返回數組中的字符串數據。由於名稱和第一個數字之間的空格數量不同,因此可以使用split("\\s+")以多個空格分隔。例如

String line = "John Smith 92 80 86 76 95"; 
String[] tokens = line.split("\\s+"); 

split將返回

tokens = { "John", "Smith", "92", "80", "76", "95" }; 

前兩個指數做出的名稱和解析指標的其餘部分。由於每一行都有相同數量的標記,因此它在循環中應該可以正常工作。

String firstName = tokens[0]; 
String lastName = tokens[1]; 
int hw = Integer.parseInt(tokens[2]); 
... 

注意見下文@janos評論了另一種選擇相同的功能

+0

我會添加用for替換for循環for(String line:data.split(「\ n」)){...}' – janos

+0

@janos我甚至都沒有看到,我的意思是'data'。你是對的,它會更清潔。 –

0

顯然,附加到95換行混淆Integer.parseInt所以你應該清除任何尾部空格的字符串第一。

相關問題