What is a NumberFormatException and how can I fix it?

前端 未结 9 1345
渐次进展
渐次进展 2020-11-21 04:25
Error Message:
Exception in thread \"main\" java.lang.NumberFormatException: For input string: \"Ace of Clubs\"
    at java.lang.NumberFormatException.forInputString         


        
9条回答
  •  悲哀的现实
    2020-11-21 04:58

    Error Message:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at 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 was an error. We try to give you as much information as possible
    It was an Exception in main thread. It's called NumberFormatException and has occurred for input "Ace of Clubs".
    at line 65th of NumberFormatException.java which is a constructor,
    which was invoked from Integer.parseInt() which is in file Integer.java in line 580,
    which was invoked from Integer.parseInt() which is in file Integer.java in line 615,
    which was invoked from method main in file Cards.java in line 68.
    
    It has resulted in exit code 1
    

    In other words, you tried to parse "Ace of Clubs" to an int what Java can't do with method Integer.parseInt. Java has provided beautiful stacktrace which tells you exactly what the problem is. The tool you're looking for is debugger and using breakpoints will allow you to inspect the state of you application at the chosen moment.

    The solution might be the following logic in case you want to 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 is an Exception in Java?

    An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

    -Documentation

    Constructors and usage in Integer#parseInt

    static NumberFormatException forInputString(String s) {
        return new NumberFormatException("For input string: \"" + s + "\"");
    }
    
    public NumberFormatException (String s) {
        super (s);
    }
    

    They are important for understanding how to read the stacktrace. Look how the NumberFormatException is thrown from Integer#parseInt:

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

    or later if the format of the input String s is not parsable:

    throw NumberFormatException.forInputString(s); 
    

    What is a NumberFormatException?

    Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

    -Documentation

    NumberFormatException extends IllegalArgumentException. It tells us that it's more specialized IllegalArgumentException. Indeed, it's used for highlighting that although, the argument type was correct (String) the content of the String wasn't numeric (a,b,c,d,e,f are considered digits in HEX and are legal when needed).

    How do I fix it?
    Well, don't fix the fact that it's thrown. It's good that it's thrown. There are some things you need to consider:

    1. Can I read the stacktrace?
    2. Is the String which causes an Exception a null?
    3. Does it look like a number?
    4. Is it 'my string' or user's input?
    5. to be continued

    Ad. 1.

    The first line of a message is an information that the Exception occurred and the input String which caused the problem. The String always follows : and is quoted ("some text"). Then you become interested in reading the stacktrace from the end, as the first few lines are usually NumberFormatException's constructor, parsing method etc. Then at the end, there is your method in which you made a bug. It will be pointed out in which file it was called and in which method. Even a line will be attached. You'll see. The example of how to read the stacktrace is above.

    Ad. 2.

    When you see, that instead of "For input string:" and the input, there is a null (not "null") it means, that you tried to pass the null reference to a number. If you actually want to treat is as 0 or any other number, you might be interested in my another post on StackOverflow. It's available here.

    The description of solving unexpected nulls is well described on StackOverflow thread What is a NullPointerException and how can I fix it?.

    Ad. 3.

    If the String that follows the : and is quoted looks like a number in your opinion, there might be a character which your system don't decode or an unseen white space. Obviously " 6" can't be parsed as well as "123 " can't. It's because of the spaces. But it can occure, that the String will look like "6" but actually it's length will be larger than the number of digits you can see.

    In this case I suggest using the debugger or at least System.out.println and print the length of the String you're trying to parse. If it shows more than the number of digits, try passing stringToParse.trim() to the parsing method. If it won't work, copy the whole string after the : and decode it using online decoder. It'll give you codes of all characters.

    There is also one case which I have found recently on StackOverflow, that you might see, that the input looks like a number e.g. "1.86" and it only contains those 4 characters but the error still exists. Remember, one can only parse integers with #Integer#parseInt#. For parsing decimal numbers, one should use Double#parseDouble.

    Another situation is, when the number has many digits. It might be, that it's too large or too small to fit int or long. You might want to try new BigDecimal().

    Ad. 4.

    Finally we come to the place in which we agree, that we can't avoid situations when it's user typing "abc" as a numeric string. Why? Because he can. In a lucky case, it's because he's a tester or simply a geek. In a bad case it's the attacker.

    What can I do now? Well, Java gives us try-catch you can do the following:

    try {
        i = Integer.parseInt(myString);
    } catch (NumberFormatException e) {
        e.printStackTrace();
        //somehow workout the issue with an improper input. It's up to your business logic.
    }
    

提交回复
热议问题