DEV Community

Teddy Zugana
Teddy Zugana

Posted on • Edited on

10 2

Java Credit Card Validation Luhn Algorithm

Any credit card number should pass following test:

From the rightmost digit, we should double every second digit. If the double is greater than 9, then add the both digits so that final number is of single digit.

Now sum all the digits in the number, the unchanged numbers and the doubled numbers.

The final sum should be multiple of 10 or mod 10 of the number should be 0. If it’s not then its not a valid credit card number.

Let’s check it with an example credit card number 12345678903555.

Digits are : 1,2,3,4,5,6,7,8,9,0,3,5,5,5
After doubling : 2,2,6,4,1,6,5,8,9,0,6,5,1,5
Sum of digits : 2+2+6+4+1+6+5+8+9+0+6+5+1+5 = 60 = 6*10 and hence a valid credit card number.
Luhn Algorithm in Java

Here I am providing java Luhn Algorithm program to validate credit card numbers.

 package com.journaldev.util;

 public class JavaLuhnAlgorithm {

public static void main(String[] args) {
    validateCreditCardNumber("12345678903555");
    String imei = "012850003580200";
    validateCreditCardNumber(imei);
}

private static void validateCreditCardNumber(String str) {

    int[] ints = new int[str.length()];
    for (int i = 0; i < str.length(); i++) {
        ints[i] = Integer.parseInt(str.substring(i, i + 1));
    }
    for (int i = ints.length - 2; i >= 0; i = i - 2) {
        int j = ints[i];
        j = j * 2;
        if (j > 9) {
            j = j % 10 + 1;
        }
        ints[i] = j;
    }
    int sum = 0;
    for (int i = 0; i < ints.length; i++) {
        sum += ints[i];
    }
    if (sum % 10 == 0) {
        System.out.println(str + " is a valid credit card number");
    } else {
        System.out.println(str + " is an invalid credit card number");
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Here I am taking input to the validation method as String, so that it will work also where the first digit is 0.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
basteez profile image
Tiziano Basile

Great explanation! I had to implement the same thing months ago, but in javascript :)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay