简体   繁体   中英

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. 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. 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

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. Since you have a different number of spaces between the name and first number, you can split by multiple spaces using split("\\\\s+") . For example

String line = "John Smith  92  80  86  76  95";
String[] tokens = line.split("\\s+");

The split will return

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

显然,附加到95的换行符使Integer.parseInt混乱,因此您应该先清除所有尾随空格的字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM