• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NumberFormatException: For input string: "0"  RSS feed

 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem trying to read in a string (its actually just 0) and converting it to an int. I keep getting : Exception in thread "main" java.lang.NumberFormatException: For input string: "0"

I tried using trim, in case there were white spaces, but that doesn't seem to work either. Here is a code snippet...

try {
Scanner whereIn = new Scanner(new FileReader("where.txt"));

where = Integer.parseInt(whereIn.next().trim());

whereIn.close();


} catch (FileNotFoundException ex) {
Logger.getLogger(character.class.getName()).log(Level.SEVERE, null, ex);
}
The only thing in the file is "0".
Thanks
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jd Wells wrote:The only thing in the file is "0".

With the quotes?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure it is a 0 and not a O?

try printing out what you are about to parse (or use a debugger and visually inspect it).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're using a Scanner, is there any reason you're not using hasNextInt() and nextInt() to read it?
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl - sorry,, without the quotes.

Matthew - when I use nextInt(), I get this - java.util.InputMismatchException.

And it is a 0 and not an o.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now you have two different pieces of code which tell you in two different ways that what you're getting from the file isn't a String representation of an integer. That just strengthens the theory that you aren't getting the digit 0, so at this point you should start considering why you aren't getting that when you think you are. Look at what you are getting from the file like this:



If you're getting the digit 0 then you should see its Unicode equivalent there, which is 48.
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.. I will try that.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
If you're getting the digit 0 then you should see its Unicode equivalent there, which is 48.


Also, it would be a good idea to print out the string length -- to confirm that it is only the character zero.

Henry
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved! Was getting extra stuff in the file that I wasnt seeing before, using one text editor. Tried a different editor and now I get 0 as I should and the file
is getting processed properly. Thanks everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!