Question

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
Was it helpful?

Solution

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

OTHER TIPS

Clearly, the line break appended to the 95 confuses Integer.parseInt so you should clear the string of any trailing whitespaces first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top