当我试图将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
是否有一些新兴的学生管理行业,或者现在的计算机课程创作者只是懒惰地思考更具创造性的任务?啊,顺便说一句,只是说出“出错”的问题描述不会帮助我们帮助你。 – qqilihq
当你使用你的调试器时,它告诉你有关传递给Integer.parseInt的值会给你带来麻烦吗? –
你从未听说过“split”吗? –