What is a NumberFormatException and how can I fix it?

前端 未结 9 1314
渐次进展
渐次进展 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:57

    int first_value = Integer.parseInt(cards[index]); 
    

    while writing the above statement, you are trying to parse "Ace of Clubs" as a number.

    you can use the following method to test if any string can be parsed as Integer:

    boolean tryParseInt(String value) {  
         try {  
             Integer.parseInt(value);  
             return true;  
          } catch (NumberFormatException e) {  
             return false;  
          }  
    }
    

    Regarding your question, what is NumberFormatException : It is 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. (ref -http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html)

提交回复
热议问题