java - What is a NumberFormatException and how can I fix it? -




error message: exception in thread "main" java.lang.numberformatexception: input string: "ace of clubs"     @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65)     @ java.lang.integer.parseint(integer.java:580)     @ java.lang.integer.parseint(integer.java:615)     @ set07102.cards.main(cards.java:68) c:\users\qasim\appdata\local\netbeans\cache\8.1\executor-snippets\run.xml:53: java returned: 1 build failed (total time: 0 seconds) 

my while loop:

while (response != 'q' && index < 52) {     system.out.println(cards[index]);     int first_value = integer.parseint(cards[index]);     int value = 0;     //add scanner     scanner scanner = new scanner(system.in);     system.out.println("will next card higher or lower?, press q if want quit");     string guess = scanner.nextline();     if(cards[index].startswith("ace")) { value = 1; }     if(cards[index].startswith("2")) { value = 2; }     if(cards[index].startswith("3")) { value = 3; }     //checking 4-10     if(cards[index].startswith("queen")){ value = 11; }     if(cards[index].startswith("king")){ value = 12; }     if(guess.startswith("h")){         if(value > first_value){ system.out.println("you answer right, weldone!"); }          else { system.out.println("you answer wrong, try again!"); }     } else if(guess.startswith("l")){         if(value < first_value) { system.out.println("you answer right, try again!"); }         else { system.out.println("you answer wrong, try again!"); }     } else { system.out.println("your not valid, try again!"); }     scanner.close();                 index++; }//end of while loop 

error message: exception in thread "main" java.lang.numberformatexception: input string: "ace of clubs"     @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65)     @ java.lang.integer.parseint(integer.java:580)     @ java.lang.integer.parseint(integer.java:615)     @ set07102.cards.main(cards.java:68) c:\users\qasim\appdata\local\netbeans\cache\8.1\executor-snippets\run.xml:53: java returned: 1 

means:

there error. try give information possible exception in main thread. it's called numberformatexception , has occurred input "ace of clubs". @ line 65th of numberformatexception.java constructor, invoked integer.parseint() in file integer.java in line 580, invoked integer.parseint() in file integer.java in line 615, invoked method main in file cards.java in line 68.  has resulted in exit code 1 

in other words, tried parse "ace of clubs" int java can't method integer.parseint. java has provided beautiful stacktrace tells problem is. tool you're looking debugger , using breakpoints allow inspect state of application @ chosen moment.

the solution might following logic in case want use parsing:

if (cards[index].startswith("ace"))      value = 1; else if (cards[index].startswith("king"))     value = 12; else if (cards[index].startswith("queen"))     value = 11; ... else {     try {         integer.parseint(string.substring(0, cards[index].indexof(" ")));      } catch (numberformatexception e){         //something went wrong     } } 

what exception in java?

an exception event, occurs during execution of program, disrupts normal flow of program's instructions.

-documentation

constructors , usage in integer#parseint

static numberformatexception forinputstring(string s) {     return new numberformatexception("for input string: \"" + s + "\""); }  public numberformatexception (string s) {     super (s); } 

they important understanding how read stacktrace. how numberformatexception thrown integer#parseint:

if (s == null) {     throw new numberformatexception("null"); } 

or later if format of input string s not parsable:

throw numberformatexception.forinputstring(s);  

what numberformatexception?

thrown indicate application has attempted convert string 1 of numeric types, string not have appropriate format.

-documentation

numberformatexception extends illegalargumentexception. tells it's more specialized illegalargumentexception. indeed, it's used highlighting although, argument type correct (string) content of string wasn't numeric (a,b,c,d,e,f considered digits in hex , legal when needed).

how fix it?
well, don't fix fact it's thrown. it's it's thrown. there things need consider:

  1. can read stacktrace?
  2. is string causes exception null?
  3. does number?
  4. is 'my string' or user's input?
  5. to continued

ad. 1.

the first line of message information exception occurred , input string caused problem. string follows : , quoted ("some text"). become interested in reading stacktrace end, first few lines numberformatexception's constructor, parsing method etc. @ end, there method in made bug. pointed out in file called , in method. line attached. you'll see. example of how read stacktrace above.

ad. 2.

when see, instead of "for input string:" , input, there null (not "null") means, tried pass null reference number. if want treat 0 or other number, might interested in post on stackoverflow. it's available here.

the description of solving unexpected nulls described on stackoverflow thread what nullpointerexception , how can fix it?.

ad. 3.

if string follows : , quoted looks number in opinion, there might character system don't decode or unseen white space. " 6" can't parsed "123 " can't. it's because of spaces. can occure, string "6" it's length larger number of digits can see.

in case suggest using debugger or @ least system.out.println , print length of string you're trying parse. if shows more number of digits, try passing stringtoparse.trim() parsing method. if won't work, copy whole string after : , decode using online decoder. it'll give codes of characters.

there 1 case have found on stackoverflow, might see, input looks number e.g. "1.86" , contains 4 characters error still exists. remember, 1 can parse integers #integer#parseint#. parsing decimal numbers, 1 should use double#parsedouble.

ad. 4.

finally come place in agree, can't avoid situations when it's user typing "abc" numeric string. why? because can. in lucky case, it's because he's tester or geek. in bad case it's attacker.

what can now? well, java gives try-catch can following:

try {     = integer.parseint(mystring); } catch (numberformatexception e) {     e.printstacktrace();     //somehow workout issue improper input. it's business logic. } 




wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

Jenkins: find build number for git commit -

firebase - How to wait value in Ionic 2 -