Scanner for long integer, Exception in thread “main” java.util.InputMismatchException

后端 未结 4 1510
情歌与酒
情歌与酒 2020-12-22 11:39

I am at the last step to finalize my program, however whenever I enter my integer (long) I get a input mismatch:

Compiler message: \"Exception in thread \"ma         


        
相关标签:
4条回答
  • 2020-12-22 11:51

    You can also convert your int to BigInteger and then call the add function on BigInteger objects

    0 讨论(0)
  • 2020-12-22 11:55

    That is because the value you're entering is beyond the range of integer values. You need to use long in this case. The max value of integer is 2147483647.

    long credNumber = kbd.nextLong();
    ..
    // in the do while loop also
    credNumber = kbd.nextLong() ;
    
    0 讨论(0)
  • 2020-12-22 11:56

    You should use:

    long credNumber = kbd.nextLong();
    boolean n = isValid(credNumber);
    

    as the value you enter (16 digits) is over the limit of int.

    Your validation method accepts long so, your code should work fine with above change.

    public static boolean isValid(long number)
    
    0 讨论(0)
  • 2020-12-22 12:10

    The maximum Integer value is 2147483647. Instead, use long:

    long credNumber = kbd.nextLong();
    

    or better use String for credit card number:

    String credNumber = kbd.nextLine();
    
    0 讨论(0)
提交回复
热议问题