简体   繁体   English

使用Integer.parseInt(String arg)时出现Java NumberFormatException

[英]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.

相关问题 使用 Integer.parseInt 时的 Java NumberFormatException - Java NumberFormatException when using Integer.parseInt 使用 Integer.parseInt 解析时 Integer 返回 NumberFormatException - Integer returning NumberFormatException when parsing using Integer.parseInt '使用 Integer.parseInt 时线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常 - Java - 'Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException' when using Integer.parseInt - Java Integer.parseInt引发的NumberFormatException - NumberFormatException thrown by Integer.parseInt Integer.parseInt 和 NumberFormatException 上 Android - Integer.parseInt and NumberFormatException on Android 尝试使用 Integer.parseInt() 将字符串转换为 integer 时出错 - Error when trying to convert string to integer using Integer.parseInt() 用于 32 位有符号二进制字符串的 Java Integer.parseInt() 抛出 NumberFormatException - Java Integer.parseInt() for 32-bit signed binary string throws NumberFormatException 为什么代表数字的String的Integer.parseInt在J2ME中抛出java.lang.NumberFormatException? - Why Integer.parseInt of a String representing a number throws java.lang.NumberFormatException in J2ME? 为什么我得到一个 Integer.parseInt(String) 一个 numberformatexception? - Why do i get for an Integer.parseInt(String) an numberformatexception? java.lang.NumberFormatException:Integer.Parseint()为null - java.lang.NumberFormatException: null with Integer.Parseint()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM