aspose file tools*
The moose likes Beginning Java and the fly likes NumberFormatException thrown from parsing string to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NumberFormatException thrown from parsing string to int" Watch "NumberFormatException thrown from parsing string to int" New topic
Author

NumberFormatException thrown from parsing string to int

Francis Low
Greenhorn

Joined: Feb 02, 2010
Posts: 5
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

Joined: Sep 29, 2008
Posts: 12617

You're not scanning past the newline.
Francis Low
Greenhorn

Joined: Feb 02, 2010
Posts: 5
Sorry, mind being more specific? I am rather new to programming.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Francis Low
Greenhorn

Joined: Feb 02, 2010
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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

Joined: Feb 02, 2010
Posts: 5
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

Joined: Feb 02, 2010
Posts: 5
Nevermind... It seems that I magically solved it
Aditya Kanitkar
Ranch Hand

Joined: Aug 08, 2009
Posts: 72
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....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NumberFormatException thrown from parsing string to int