Jump to content


Numberformatexception Help


  • Please log in to reply
2 replies to this topic

#1 SoiledShip

SoiledShip

    Member

  • Members
  • PipPip
  • 16 posts
  • Gender:Male
  • Location:Texas
  • Interests:Computers, cars, loud music, and plenty of fun

Posted 26 January 2010 - 02:32 AM

I'm working on a USACO program and i have it working all except for this stupid NumberFormatException, the program is to calculate palindromic squares when given a base between 2 & 20. this is the line spitting the error
CODE
int i = ctr+1;
String str = Integer.toString(i);
int b = Integer.parseInt(str,base);

Will this not work? it throws the error when base!=10.

#2 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 26 January 2010 - 10:22 AM

If your base is lower than 10 how do you expect digits equal to or larger than your base number to be parsed correctly?
Example; with base = 2 any number containing 2,3,...,9 will rightfully throw an exception.

#3 SoiledShip

SoiledShip

    Member

  • Members
  • PipPip
  • 16 posts
  • Gender:Male
  • Location:Texas
  • Interests:Computers, cars, loud music, and plenty of fun

Posted 28 January 2010 - 02:17 AM

i should have removed this, sorry. a friend of mine explained to me that Integer.parseInt() has a built in base conversion to goto base 10. but Integer.toString() has a built in base conversion to go from base 10 to what ever you want. i just had to switch parseInt() with toStirng(). thanks for the help anyway blush.gif