0

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
3
  • Is there some new emerging student management industry, or are computer course creators just lazy nowadays to think about more creative tasks? Ah, by the way, a problem description just stating "getting an error" won't help us to help you. Commented Feb 16, 2014 at 15:54
  • When you use your debugger, what is it telling you about the value you're passing to the Integer.parseInt that is giving you trouble? Commented Feb 16, 2014 at 15:54
  • 2
    have you never heard of split? Commented Feb 16, 2014 at 15:55

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I would add to replace the for loop with for (String line : data.split("\n")) { ... }
@janos I didn't even see that, the data I mean. You're right though, it would be cleaner that way.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.