长整数扫描程序,线程中出现异常;“主要”;java.util.InputMismatchException
我正处于完成程序的最后一步,但是,每当我输入整数(长)时,就会出现输入不匹配:长整数扫描程序,线程中出现异常;“主要”;java.util.InputMismatchException,java,java.util.scanner,long-integer,credit-card,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(Sc
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 ;
}
}
import java.util.Scanner//导入扫描仪
公共类信用卡验证{
公共静态void main(字符串[]args){
扫描仪kbd=新扫描仪(System.in);
System.out.println(“请输入信用卡号:”);
int credNumber=kbd.nextInt();
布尔n=isValid(credNumber);
如果(credNumber==0)
系统出口(0);
做{
System.out.println(“请输入信用卡号:”);
credNumber=kbd.nextInt();
}
而(credNumber<0);
如果(credNumber>0)
系统输出打印(“此信用卡号为”+n);
}
/*
如果号码是有效的卡号,则返回true。
*/
公共静态布尔值isValid(长数字){
长p=getPrefix(数字,1);
长p2=getPrefix(数字,2);
int n=getSize(数字);
如果((p==4 | | p==5 | | p==6 | | p2==37)和&(n<13 | | n>16)和&((sumOfDoubleEvenPlace(数字)+sumOfoddPlace(数字))%10)==0)
返回true;
其他的
返回false;
}
/*从第一个数字开始,每隔一个数字的总和加倍*/
公共静态内景双倍空间(长数字){
整数和=0;
int-maxDigittenth=16;
对于(int i=1;i最大整数值为2147483647。请使用long:
long credNumber = kbd.nextLong();
或者最好使用字符串作为信用卡号:
String credNumber = kbd.nextLine();
最大整数值为2147483647。请改用长:
long credNumber = kbd.nextLong();
或者最好使用字符串作为信用卡号:
String credNumber = kbd.nextLine();
最大整数值为2147483647。请改用长:
long credNumber = kbd.nextLong();
或者最好使用字符串作为信用卡号:
String credNumber = kbd.nextLine();
最大整数值为2147483647。请改用长:
long credNumber = kbd.nextLong();
或者最好使用字符串作为信用卡号:
String credNumber = kbd.nextLine();
你应使用:
long credNumber = kbd.nextLong();
boolean n = isValid(credNumber);
因为您输入的值(16位)超过了int的限制
您的验证方法接受的时间很长,因此,您的代码应该可以很好地处理上述更改
public static boolean isValid(long number)
你应使用:
long credNumber = kbd.nextLong();
boolean n = isValid(credNumber);
因为您输入的值(16位)超过了int的限制
您的验证方法接受的时间很长,因此,您的代码应该可以很好地处理上述更改
public static boolean isValid(long number)
你应使用:
long credNumber = kbd.nextLong();
boolean n = isValid(credNumber);
因为您输入的值(16位)超过了int的限制
您的验证方法接受的时间很长,因此,您的代码应该可以很好地处理上述更改
public static boolean isValid(long number)
你应使用:
long credNumber = kbd.nextLong();
boolean n = isValid(credNumber);
因为您输入的值(16位)超过了int的限制
您的验证方法接受的时间很长,因此,您的代码应该可以很好地处理上述更改
public static boolean isValid(long number)
这是因为您输入的值超出了整数值的范围。在这种情况下,您需要使用long。integer的最大值为2147483647
这是因为您输入的值超出了整数值的范围。在这种情况下,您需要使用long。integer的最大值为2147483647
这是因为您输入的值超出了整数值的范围。在这种情况下,您需要使用long。integer的最大值为2147483647
这是因为您输入的值超出了整数值的范围。在这种情况下,您需要使用long。integer的最大值为2147483647
您还可以将int转换为biginger,然后在biginger对象上调用add函数您还可以将int转换为biginger,然后在biginger对象上调用add函数您还可以将int转换为biginger,然后在biginger对象上调用add函数您还可以将int转换为BigInteger,然后对biginger对象调用add函数(因为它太长了),因为它太长了(因为它太长了),因为它太长了(因为它太长了)