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

穿精又带淫゛_ 提交于 2019-11-29 18:07:06

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() ;

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();

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!