Skip to content
Advertisement

How would I add two int that are in the same array to each other and convert them into an int. In the Luhn Algorithm

I am trying to add two parts of an array together to go into an int value. I am using Luhn algorithm to figure out of a credit card is a valid credit card. We are only using 6 digit credit card’s just to make sure no one enter’s a real credit card number. The part I am confused on is when I go to split a number that is above 10 and add it together. Example if the algorithm was to give me 12 I would need to separate it into 1 and 2 and then add them together to equal 3. I believe I am splitting it currently in the code but when I go to add them together I get some number that makes no since. here is a section of the code with some notes about it.

I have printed out numbers in certain places to show myself what is going on in certain places. I have also added in some comments that say that either the number that is printed out is what is expected, and some comments for when there isn’t something I expected

int[] cardNumber = new int[]{ 1,2,3,4,5,5};
    int doubleVariablesum = 0;
    int singleVariablesum = 0;
    int totalSum = 0;
    int cutOffVar = 0;
    String temp2;
    for (int i = cardNumber.length - 1; i >= 0;) { 
        int tempSum = 0;
        int temp = cardNumber[i];
        temp = temp * 2;
        System.out.println("This is the temp at temp * 2: " + temp);
        temp2 = Integer.toString(temp);
        if (temp2.length() == 1) { 
            System.out.println("Temp2 char 0: "+ temp2.charAt(0));
            // this prints out the correct number  
            // Example: if there number should be 4 it will print 4

            tempSum = temp2.charAt(0);  
            System.out.println("This is tempSum == 1: " + tempSum);
            // when this goes to add temp2.charAt(0) which should be 4 it prints out                      //something like 56

        } else {
            System.out.println("TEMP2 char 0 and char 1: " + temp2.charAt(0) + " " + temp2.charAt(1));

// this prints out the correct number successfully spited

            tempSum = temp2.charAt(0) + temp2.charAt(1);
            System.out.println("This is tempSum != 1: " + tempSum);
            // but here it when I try to add them together it is giving me something 
            // like 97 which doesn't make since for the numbers I am giving it
        }
        doubleVariablesum = tempSum + doubleVariablesum;
        System.out.println("This is the Double variable: " + doubleVariablesum);
        System.out.println();
        i = i - 2;
    }

Advertisement

Answer

Since you are converting the number to a string to split the integer, and then trying to add them back together. You’re essentially adding the two characters numerical values together which is giving you that odd number. You would need to convert it back to an integer, which you can do by using Integer.parseInt(String.valueOf(temp2.charAt(0)))

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement