Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sum of three digit calculator-  RSS feed

 
Konrad Cetes
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time the user input a three digit number it comes back with error messages

 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post your error message. I don't get an error. What input were you giving it?
 
Konrad Cetes
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what comes up when the program is ran

Exception in thread "main" java.lang.NumberFormatException: For input string: " 897"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at com.UserInputs.Main.main(Main.java:14)
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Konrad Cetes wrote:This is what comes up when the program is ran

Exception in thread "main" java.lang.NumberFormatException: For input string: " 897"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at com.UserInputs.Main.main(Main.java:14)

Ah, no leading space allowed. Use trim() on the String before parsing.
 
Konrad Cetes
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm new to java, where do i put trim()
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mynum = Integer.parseInt( myInput.readLine().trim() );
 
Konrad Cetes
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, works now
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be easier to use a Scanner to read from the keyboard.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!