credit card validation in java using Luhn Formula for validation here is the cre
ID: 3770968 • Letter: C
Question
credit card validation in java using Luhn Formula for validation
here is the credit card numbers to evaluate
Data to Evaluate
3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6381475006869978
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
6011824617460743
6011824617460745
6389057917814802
4539318768050385
36880982786892
36845793345501
36661154562232
36661154562234
5893329089846994
6761680165952016
6763100400984029
6763100400984022
5127043299122389
5330838017737340
5429168755154025
5429168755154023
375354034606481
379570632133224
4485521241443135
4532916206508075
4532916206508076
5590976687287124
5540641137519895
5540641137519892
30522070708059
30066552673241
30365487186802
30365487186801
here is how to organize the numbers
VISA:
45
44
MasterCard:
51
53
American Express (AMEX):
37
34
Discover:
60
JCB:
31
33
Diners Club - North America:
54
55
Diners Club - Carte Blanche:
30
Diners Club - International:
36
Maestro:
58
LASER:
67
Visa Electron:
48
49
InstaPayment:
63
Explanation / Answer
import java.util.Scanner;
public class CreditCard
{
public static void main(String[] args)
{
long total;
Scanner input = new Scanner(System.in);
System.out.print("Please input your credit card number: ");
long number = input.nextLong();
System.out.print("Please input the number of digits you would like to see: ");
int k = input.nextInt();
total = sumOfEvenPlaces(number) + sumOfOddPlaces(number);
System.out.println(total);
if(isValid(total)) {
System.out.println("The length of your card number is: " + getSize(number));
System.out.println("The first k number of digits of your credit card are: " + getPrefix(number, k));
System.out.println("This card is valid.");
}
else
{
System.out.println("The length of your card number is: " + getSize(number));
System.out.println("The first k number of digits of your credit card are: " + getPrefix(number, k));
System.out.println("Your card is invalid.");
}
}
public static boolean isValid(long total)
{
if (total % 10 == 0)
{
return true;
}
return false;
}
public static int sumOfEvenPlaces(long number)
{
int sum = 0;
number = number / 10;
while (number != 0)
{
sum += getDigit((int)((number % 10) * 2));
number = number / 100;
}
System.out.println(sum);
return sum;
}
public static int getDigit(int number)
{
if (number <= 9)
{
return number;
}
else if (number > 9)
return (number % 10 + number / 10);
return number;
}
public static int sumOfOddPlaces(long number)
{
int sum = 0;
while (number != 0)
{
sum += (int)(number % 10);
number = number / 100;
}
System.out.println(sum);
return sum;
}
public static boolean getPrefix1(long number, int k)
{
return getPrefix(number, getSize(number)) == number;
}
public static int getSize(long number)
{
int len = 0;
while (number >= 10)
{
number /= 10;
len++;
}
return len;
}
public static long getPrefix(long number, int k)
{
long result = number;
for (int i = 0; i < getSize(number) - (k - 1); i++)
result /= 10;
return result;
}
}
Easy way:
public class Luhn
{
public static boolean Check(String ccNumber)
{
int sum = 0;
boolean alternate = false;
for (int i = ccNumber.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(ccNumber.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.