Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: HELP! Luhn Algorithm Credit Card Validation

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP! Luhn Algorithm Credit Card Validation

    Hi Every One
    Hope you're doing well !

    any how

    i'm really desperate i need a java program
    the promotes the user to enter a Credit Card as a long number
    then check for validation using Luhn Algorithm which i need to use methods for luhn rules .
    I also have to take the long number of the credit card and assign it into an array after i apply
    luhn rules in other methods .

    hope you understand
    and hope you heeeeeelp me pleeeeeeeeeeeeeeeeeas

    Thanxx .


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HELP! Luhn Algorithm Credit Card Validation

    We understand, but what have you tried?

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Luhn Algorithm Credit Card Validation

    i didn't know how to take the variable credit card number and assign it to an array after i'm finished ?


    this is my code i took the credit card number as an array from the begging
    and that's wrong .
    i'm stuck

    import java.util.Scanner ;
    public class creditValidation {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    long[] cardNmbers = new long [16];

    do {
    System.out.print("Enter a card number with 16 digit : ");
    for (int i=0 ; i<cardNmbers.length ; i++ ){
    cardNmbers[i] = input.nextInt();
    multplyAndSum(cardNmbers[i]);
    addOdd(cardNmbers[i]);
    int summation = (int) (multplyAndSum(cardNmbers[i]) + addOdd(cardNmbers[i]));
    System.out.println(cardNmbers[i] + "\n" + isTrue(cardNmbers[i],cardNmbers[i]));

    }
    }
    while ((cardNmbers[0]!=4) ||(cardNmbers[0]!=5) || (cardNmbers[0]!=3 && cardNmbers[1]!=7) );
    }



    public static int multplyAndSum(long[] num1){
    long num = long[]num1 ;
    int sum = 0 , rem = 0 ;
    while (num>0)
    rem=rem%10;
    num = num/10;
    sum = sum * 10 + rem ;
    System.out.println(num*2);
    if (num>9){
    num= num % 10 + num / 10;
    }
    sum +=num ;
    return (int) sum ;
    }

    public static long addOdd (long [] oddNum){

    int sum = 0;
    int rem=0;
    int count = 0 ;

    while (oddNum>0){
    oddNum=oddNum/10;
    rem=rem%10;
    count++;
    if (count%2==1)
    sum+=oddNum;

    }
    return sum;

    }

    public static boolean isTrue(long odd , long even){
    multplyAndSum(even);
    addOdd(odd);
    int summation = (int) (multplyAndSum(even) + addOdd(odd));
    if (summation%10==0)
    return true ;
    else
    return false ;
    }

    }

    ^ it's all wrong :'(

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HELP! Luhn Algorithm Credit Card Validation

    Read this FAQ for instructions on how to post code correctly and other useful tips for newcomers. Also, tell us what you think is wrong or what you'd like help with. Post errors, sample runs, whatever it takes to explain what's wrong with your code or why you're stuck and ask specific questions about what you don't understand that will help you move forward.

    This sentence, "i didn't know how to take the variable credit card number and assign it to an array after i'm finished," doesn't make sense. You might look at the String API to get ideas on what can be done with String objects to accomplish what you're trying to do.

  5. #5
    Junior Member
    Join Date
    Dec 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Luhn Algorithm Credit Card Validation

    Luhn's algorithm determines whether or not a credit card number is valid. For a given credit card number: Double the value of every other digit from right to left, beginning with the second to last digit. Add the digits of the results of Step 1 to the remaining digits in the credit card checker..... https://seotoolsystem.com/credit-card-validator/

    To calculate the check digit, multiply every even-position digit (when counted from the right) in the number by two. If the result is a two digit number, then add these digits together to make a single digit (this is called the digital root).

    The formula is widely used in validating credit card numbers, as well as other number sequences such as government Social Security Numbers (SSNs). Today, the Luhn Algorithm is an essential component in the electronics payments system and is used by all major credit cards.
    Last edited by rjpruh9@; December 9th, 2022 at 08:19 AM.

Similar Threads

  1. Luhn Algorithm(Mod10) Help (Beginner)
    By IBeeJava in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 1st, 2013, 09:12 AM
  2. Credit Card String Program
    By hotshotennis in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 6th, 2012, 04:17 PM
  3. Credit card help
    By kostas198 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2011, 10:50 PM
  4. Credit Card Problem (While and Switch will be used)
    By odun in forum Object Oriented Programming
    Replies: 3
    Last Post: March 29th, 2011, 12:54 AM
  5. Credit card validator
    By SOK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 03:11 AM