Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keep getting this error and not sure why

 
Chris Wells
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone can take a few minutes and help me, I would be unbelievably grateful. I have been staring at this program for hours now and the solution is probably pretty simple.

My program looks like it should work perfectly and it does not register any errors until I run it.
Then, when I run it, I get:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Chris"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at grade_book_methods.load_array(grade_book_methods.java:32)
at grade_book_methods.main(grade_book_methods.java:10)

 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NumberFormatException: For input string: "Chris"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at grade_book_methods.load_array(grade_book_methods.java:32)
at grade_book_methods.main(grade_book_methods.java:10)


The stacktrace pretty much tells you everything... In your code, the grade_book_methods class, you have a method called load_array(). In line 32 of the java file, within that method, you call the parseInt() method, which is used to convert a string into an integer.

However, the string that you passed is "Chris", which is not a valid integer, and generated a NumberFormatException.

Henry
 
Chris Wells
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for responding Henry.

After I figure out how to correct that error, is that the only one I am going to have to worry about?

What about the other four lines in the error message?
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I figure out how to correct that error, is that the only one I am going to have to worry about?

What about the other four lines in the error message?


It's a stack trace, the whole error message is one error. Once you get comfortable with reading it, you'll learn to ignore what is not relevant.

But if you want to know what it is saying...

-- The load_array() method was called from the main() method, at line 10.
-- Within the parseInt() method that you called, it called an overloaded parseInt() method.

Generally, what is important is where your code left off, and where the APIs start. It is not that important to look at the chain of methods within the API. And how you got to the load_array() method is only important, if you didn't expect it to be called that way.

Henry
 
Chris Wells
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, thank you for being so helpful tonight. I really appreciate your time.

I am playing around with my program, now that I know what the error means, and it seems that whatever I do, the error is still popping up, in different ways. Surely, there has to be some way to read in names and numbers.

I cannot get rid of the parseInt because the variable is an int and it will give me an error of having to convert an int to a String.

I am reading the data in from grades_in.txt which looks like:

Chris
84
90
Laura
95
100
Veronica
72
85
Mike
55
78
Bert
76
79
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your code reads the two grades first, then reads the name -- while the text file has the name first, then the two grades.

Henry
 
Chris Wells
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not seem that way. It runs the column in the loop first (which is definied as [5], and then the row, and the row is defined as [2].

You seem like an experienced programmer. Can you point me in the right direction? I feel like I have been looking at this so long I am not looking at it right.
 
Chris Wells
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, I got it to work!!! Thank you so much!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic