[英]Java NumberFormatException when using Integer.parseInt(String arg)
I am getting NumberFormatException when trying to parse the last part of a String to an integer. 尝试将String的最后一部分解析为整数时,出现NumberFormatException。 The exception prints as follows: 异常打印如下:
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)
I am running a for loop to break down a long String into parts and then create objects from those parts. 我正在运行for循环,以将长字符串分解成多个部分,然后从这些部分创建对象。 Here is the method: 方法如下:
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;
}
Thank you so much for any help! 非常感谢您的帮助!
I begin with a String data which if I System.out.print(data) produces 我从String数据开始,如果我System.out.print(data)产生
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
I strongly suggest you use String#split
which returns an array. 我强烈建议您使用String#split
返回数组。 Since you have a different number of spaces between the name and first number, you can split by multiple spaces using split("\\\\s+")
. 由于名称和第一个数字之间的空格数不同,因此可以使用split("\\\\s+")
拆分多个空格。 For example 例如
String line = "John Smith 92 80 86 76 95";
String[] tokens = line.split("\\s+");
The split
will return split
将返回
tokens = { "John", "Smith", "92", "80", "76", "95" };
The first two indices make the name and parse the rest of the indices. 前两个索引命名并解析其余索引。 Since every line has the same number of tokens, it should work fine for you in a loop. 由于每一行都有相同数量的令牌,因此在循环中它应该可以正常工作。
String firstName = tokens[0];
String lastName = tokens[1];
int hw = Integer.parseInt(tokens[2]);
...
Note see @janos comment below for another option for the same functionality 注意,请参阅下面的@janos评论,以获取具有相同功能的另一个选项
显然,附加到95
的换行符使Integer.parseInt
混乱,因此您应该先清除所有尾随空格的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.