Home > Back-end >  Have who can help me have a look at what's the problem? For help
Have who can help me have a look at what's the problem? For help

Time:11-02

Procedure is as follows:
import java.util.Scanner;

Public class ExamIDcard_31 {

Public static void main (String [] args) {
Scanner input=new Scanner (System. In);
System. The out. Print (" input 16 card number: ");
Long number=input. NextLong ();
If (isValid (number)) {
System. The out. Print (" card number qualified ");
}
The else {
System. The out. Print (" unqualified number ");
}

}

Public static Boolean isValid (number) {//judge the input number is comply with the regulations of bank card
Int size=String. The valueOf (number). The length ();
If (size & gt;=13 & amp; & The size & lt;=16 & amp; & (sumOfOddPlace (number) + sumOfDoubleEvenPlace (number)) % 10==0)
return true;
The else
return false;
}

Public static int sumOfDoubleEvenPlace (number) {//return double dual digital Numbers from left to right, and then proceeds to all single digits addition and
Int sumEven=0;
Int Even;
Int EvenNew;
Char Evenchar;
for(int i=0; I & lt;=String. The valueOf (number). The length (); I=I + 2) {
Evenchar=String. The valueOf (number). CharAt (I);
Even=Integer. ParseInt (String. The valueOf (number));
If (Even * 2 & gt;=10) {
EvenNew=Even * 2-9;
}
The else {
EvenNew=Even * 2;
}
SumEven +=EvenNew;
}
Return sumEven;
}


Public static int sumOfOddPlace (number) {//card number of odd bits from right to left in the sum of the number of each
Int sumOdd=0;
Int Odd;
Char Oddchar;
String s=String. The valueOf (number);
For (int I=15; I & gt;=1; I={I - 2)
Oddchar=String. The valueOf (number). CharAt (I);
Odd=Integer. ParseInt (String. The valueOf (number));
SumOdd +=Odd;
}
Return sumOdd;
}

Public static int getSize (String number)//return card length
{
Int Size;
Size=String. The valueOf (number). The length ();
Return the Size;
}


}

The results are as follows:
Input 16 card number: 4388576018402626
The Exception in the thread "main" Java. Lang. A NumberFormatException: For input string: "4388576018402626"
The at Java. Lang. A NumberFormatException. ForInputString (65). A NumberFormatException Java:
The at Java. Lang. Integer. ParseInt (Integer. Java: 583)
The at Java. Lang. Integer. ParseInt (Integer. Java: 615)
The at game1. ExamIDcard_31. SumOfOddPlace (ExamIDcard_31. Java: 56)
The at game1. ExamIDcard_31. IsValid (ExamIDcard_31. Java: 23)
The at game1. ExamIDcard_31. Main (ExamIDcard_31. Java: 12)

CodePudding user response:

Number format is unusual, the input number is too big, can use the string to accept input number
  • Related