2014-02-16 71 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所以你应该清除任何尾部空格的字符串第一。

相关问题