Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java int扫描仪可接受的最大位数_Java_Java.util.scanner - Fatal编程技术网

Java int扫描仪可接受的最大位数

Java int扫描仪可接受的最大位数,java,java.util.scanner,Java,Java.util.scanner,我正在尝试读取用户输入的信用卡号。然而,在输入10位数字后,我得到了一个输入错误匹配。对于我的代码,任何低于10位的都可以 do{ System.out.print("Please enter your creditcard number:"); ccNum = scan4.nextInt(); int length = String.valueOf(ccNum).length(); if(length !=12) { ccNumInfo

我正在尝试读取用户输入的信用卡号。然而,在输入10位数字后,我得到了一个输入错误匹配。对于我的代码,任何低于10位的都可以

 do{

    System.out.print("Please enter your creditcard number:");
    ccNum = scan4.nextInt();
    int length = String.valueOf(ccNum).length();
    if(length !=12)
    {
       ccNumInfo=false;
        System.out.println("Please enter a 12 digit card number");
    }
  }while(ccNumInfo!= true); 

这不是扫描仪的问题,而是对Java的
int
的限制。作为一个32位数字,它不接受超出其最大值2147483647的值

如果需要12位数字,请改用
long
。它是一个64位数字,上限为
9223372036854775807


使用字符串并验证其是否仅包含数字可能是读取和存储卡号的另一个可接受的解决方案:它甚至适用于具有前导零的12位卡号(无论是
long
还是
int
都不会在您的号码中保留前导零)。

谢谢您的回复。很抱歉,我没有机会与许多价值观一起工作来认识到这一点。