- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当尝试将字符串的最后部分解析为整数时,我收到 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
最佳答案
我强烈建议您使用String#split
它返回一个数组。由于姓名和第一个数字之间的空格数量不同,因此您可以使用 split("\\s+")
分割多个空格。例如
String line = "John Smith 92 80 86 76 95";
String[] tokens = line.split("\\s+");
分割
将返回
tokens = { "John", "Smith", "92", "80", "76", "95" };
前两个索引生成名称并解析其余索引。由于每行都有相同数量的标记,因此它应该可以在循环中正常工作。
String firstName = tokens[0];
String lastName = tokens[1];
int hw = Integer.parseInt(tokens[2]);
...
<小时/>
注意请参阅下面的 @janos 评论,了解具有相同功能的另一个选项
关于使用 Integer.parseInt(String arg) 时出现 Java NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813505/
来自 documentation : This method behaves identically to the global function parseInt() 但是,由于它是实验性的,the
这个问题在这里已经有了答案: Javascript parseInt() with leading zeros (7 个答案) 关闭 8 年前。 在IE8中发现了一件奇怪的事情: parseInt(
我对 parseInt 的特定结果感到困惑。我以为我了解它的大部分怪癖,但这一个还不清楚。 parseInt("010", 10); // 10 parseInt(010, 10); // 8,
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: JavaScript function parseInt() doesn't parse numbers w
这个问题纯属好奇。 通过控制台 parseInt(1111111111111111,2) // 16 1's 返回 65535 和 parseInt(11111111111111111,2) // 1
我正在开发一个 Java 项目,将每个整数添加到下一行中的整数,直到文件中没有任何行可供读取。因此,为了能够添加它,我必须在该行中使用 Integer.parseInt(...) 然后添加它。P.S:
在 MDN 的 Number.parseInt部分,它说: The Number.parseInt() method parses a string argument and returns an i
为什么 parseInt('60', 10) 与 parseInt('60', 0) 相同? JavaScript 将 0 基数转换成什么? 最佳答案 来自 MDN ( https://develop
在阅读 JavaScript 中的 NaN 时,我遇到了一些示例。然后我开始进行更多测试,结果让我感到惊讶。例如,我想知道为什么 JavaScript 对下面两个几乎相同的语句的解释不同 parseI
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 为什么 parseInt
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我认为它与八进制
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我正在研究 jav
我正在阅读 this但我对 parseInt 中带有基数参数一章的内容感到困惑 为什么是 parseInt(8, 3) → NaN 和 parseInt(16, 3) → 1? AFAIK 8 和 1
我必须构建一个由我构建的一些结构组成的数组: 类(Item)的构建方式如下:(如下所示) 现在我的问题是我试图将从另一个地方(在本例中为列表)获取的一些字符串数字解析为 int。 但我收到此错误: T
我是 Javascript 新手,有一个关于使用 parseInt() 函数将字符串转换为数字的问题。 我通过编写以下代码来练习 parseInt,并在将用户输入从字符串变为数字之前使用 typeof
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Workarounds for JavaScript parseInt octal bug 我正在解析一个字
我有这个 jQuery 片段,用于从输入字段获取 ID 号 $('table th input').change(function() { var id = $(this).attr('id'
我有这个 JavaScript 文件(Rhino 1.7R4)。 importPackage(java.io); importPackage(java.lang); importPackage(jav
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
当我尝试使用 parseInt(value) 将其转换为 int 时,我有 var value="10+10" ,它给了我 NaN。如果字符串中有特殊字符,他们是否可以选择转换字符串?结果应为 20
我是一名优秀的程序员,十分优秀!