Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException thrown from parsing string to int

 
Francis Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting a NumberFormatException when the following method is ran



This is how the scanned file looks like on notepad



This is how Netbeans displayed the error



Anyone cares to enlighten me?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not scanning past the newline.
 
Francis Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, mind being more specific? I am rather new to programming.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francis Low wrote:Sorry, mind being more specific? I am rather new to programming.


What you got is... a string that contains a carriage return followed by the letter "1". The carriage return in the string is not valid number digit.

Henry
 
Francis Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Francis Low wrote:Sorry, mind being more specific? I am rather new to programming.


What you got is... a string that contains a carriage return followed by the letter "1". The carriage return in the string is not valid number digit.

Henry


Ah... Any idea how to rid that extra part? Or is there another way to scan the file?

Thanks in advance!
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francis Low wrote:
Ah... Any idea how to rid that extra part? Or is there another way to scan the file?


Either will work... the token returned is a string, so you can use the java.lang.String class API to get rid of it. The useDelimiter() method takes a regex, so you can modify the regex to include the optional carriage return. And finally, you can use something in the Scanner class to get the CR first. You have lots of options here.

Henry
 
Francis Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this



then parse



Still doesn't seem to work. The number seems to not be recognised as a character. The exception thrown is the same as above, except the input this time is



instead of



This is getting annoying
 
Francis Low
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind... It seems that I magically solved it
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know by which way you've solved it.... but let me give you one more hint...

Theres one method in String Class... that method has no arguments....
And this method is also one of the best solutions for your problem....

You may guess it quickly....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic