java - 扫描长整数,线程中出现异常 "main"java.util.InputMismatchException

标签 java java.util.scanner long-integer credit-card inputmismatchexception

我正处于完成程序的最后一步,但是每当我输入整数(长整型)时,我都会遇到输入不匹配的情况:

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)"

我的代码如下:

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 ;

    }

}

感谢您的宝贵时间!

最佳答案

这是因为您输入的值超出了整数值的范围。在这种情况下你需要使用 long。整数的最大值为2147483647

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

关于java - 扫描长整数,线程中出现异常 "main"java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19698025/

相关文章:

java - JAVA中SSL握手时如何获取ClientHello信息

java - java中不同线程可以同时访问同一个对象的不同独立方法吗?

java - 扫描仪拾取垃圾值?

java - java中的拼字游戏数组

java - 在不损失精度的情况下将 long 或 BigInteger 乘以 double

c - 大量输入的“因超时而终止”

Java System.out.print 格式化

java - 您如何维护 wicket 中面板的样式信息?

java - 扫描仪读取数据文件 Java.Util.NoSuchElementException : Null

c - 十六进制数对于 long long int 来说太大了吗?