长整数扫描器,线程“main”中的异常 java.util.InputMismatchException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19698025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:29:45  来源:igfitidea点击:

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

javajava.util.scannerlong-integercredit-cardinputmismatchexception

提问by Dan Gurewitch

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 "main" java.util.InputMismatchException: For input               string: "4388576018402626"
at java.util.Scanner.nextInt(Scanner.java:2097)
at java.util.Scanner.nextInt(Scanner.java:2050)
at CreditCardValidation.main(CreditCardValidation.java:12)"

My code is as follows:

我的代码如下:

import java.util.Scanner ; //import Scanner

public class CreditCardValidation {

public static void main (String[] args){

    Scanner kbd = new Scanner(System.in) ;

    System.out.println("Please enter Creditcard number: " ) ;

    int credNumber = kbd.nextInt() ;

    boolean n = isValid( credNumber ) ;

if (credNumber == 0 )
    System.exit(0) ;

do {                

    System.out.println("Please enter Creditcard number: " ) ;
    credNumber = kbd.nextInt() ;
    }
    while ( credNumber < 0 ) ;

if (credNumber > 0 )
    System.out.print("This credit card number is " + n ) ;




}

/*
Return true is the number is a valid card number.
*/

public static boolean isValid(long number) {


    long p = getPrefix(number, 1);
    long p2 = getPrefix(number, 2);
    int n = getSize(number);


    if ((p == 4 || p == 5 || p == 6 || p2 == 37)&& (n < 13 || n > 16) && (((sumOfDoubleEvenPlace(number) + sumOfoddPlace(number))) % 10) == 0)
        return true ;
    else
        return false ;

}


/* The sum of every other digit, doubled, starting with the first digit. */

public static int sumOfDoubleEvenPlace(long number) {
    int sum = 0;
    int maxDigitLenth = 16;
    for (int i = 1; i <= maxDigitLenth; i++)
    {
        if (i % 2 == 0)
        {
            sum = sum + getDigit((int)(number % 10) * 2);
        }
        number /= 10;
    }
    return sum;
}

/*
Return the number if it is 0-9, otherwise return the sum of
the digits of the number.
*/

public static int getDigit(int number) {
    if (number < 10) {

        return number;
    }
    else {

        return (number / 10) + (number % 10);
    }

    }
/*
Return the sum of the odd-place digits.
*/

public static int sumOfoddPlace(long number) {
    int maxDigitLength = 16;
    int sum = 0;
    for (int i = 1; i <= maxDigitLength; i++)
    {

        if (i % 2 == 1)
        {
            sum = sum + (int)(number % 10);
        }
       number /= 10;
    }
    return sum;
}

/*
Return the number of digits in d
*/

public static int getSize(long d) {

    int size = 0 ;

    while( d > 0 ) {
        d = d / 10 ;
        size = size + 1 ;       
    }
    return size ;
}

/*
Return the first k number of digits from number. If the number of digits in number is 
less than k, return the number.
*/

public static long getPrefix(long n, int k) {

    int f = getSize(n)-k;

    long prefix = n/((long)(Math.pow(10, f)));

    return prefix;
    }

/*
Return true if the digit d is a prefix for number.
*/

public static boolean prefixMatched( long number, int d ) {

    if ( d == getPrefix(number, 4))

        return true ;
    else
        return false ;

    }

}

Thank you for your time!

感谢您的时间!

采纳答案by SudoRahul

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。整数的最大值为2147483647

long credNumber = kbd.nextLong();
..
// in the do while loop also
credNumber = kbd.nextLong() ;

回答by Eng.Fouad

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

最大整数值是 2147483647。相反,使用 long:

long credNumber = kbd.nextLong();

or better use String for credit card number:

或者更好地使用 String 作为信用卡号:

String credNumber = kbd.nextLine();

回答by Akshay

You should use:

你应该使用:

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

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

因为您输入的值(16 位)超过了 int 的限制。

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

您的验证方法接受很长时间,因此您的代码应该可以在上述更改中正常工作。

public static boolean isValid(long number)

回答by ShrutiK

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

您还可以将 int 转换为 BigInteger,然后在 BigInteger 对象上调用 add 函数