Home > database >  How would I add two int that are in the same array to each other and convert them into an int. In th
How would I add two int that are in the same array to each other and convert them into an int. In th

Time:09-24

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;
    }

CodePudding user response:

When adding char symbols '0' and '1' their ASCII values are added - not numbers 0 and 1.

It is possible to use method Character::getNumericValue or just subtract '0' when converting digit symbol to int.

However, it is also possible to calculate sum of digits in a 2-digit number without any conversion to String and char manipulation like this:

int sum2digits = sum / 10   sum % 10; // sum / 10 always returns 1 if sum is a total of 2 digits

CodePudding user response:

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)))

CodePudding user response:

Seems like charAt() type casts into integer value, but the ascii one. Hence for the characters '0' and '1', the numbers 48 and 49 are returned resulting in a sum of 97. To fix this, you could just assign temp2 to (temp / 10) (temp % 10). Which actually splits a two digit integer and adds their sum.

CodePudding user response:

You need to be aware of the following when dealing with char and String

  1. Assigning the result of charAt(index) to an int will assign the ASCII value and not the actual integer value. To get the actual value you need to String.valueOf(temp2.charAt(0)).

  2. The result of concatenating chars is the sum of the ASCII values. eg if char c = '1'; System.out.println(c c); will print "98" not "11". However System.out.println("" c c); will print "11". Note the "" will force String concatenation.

  • Related