0

i have a bit of strange confusion happening when trying to convert my char variables from my char array into ints.

Here is my code:

public class Luhn {

private String cardNumber;
private char[] cardArray;

public Luhn(String cardNumber) {
    this.cardNumber = cardNumber;
    cardArray = new char[cardNumber.length()];

    for (int i = 0; i < cardNumber.length(); i++) {
        cardArray[i] = cardNumber.charAt(i);
    }


public void calculation() {
    for(int i = cardArray.length-1; i >= 0; i-=2) {
        char y = cardArray[i];
        int x = (int) (cardArray[i] * 1);
        if(x > 9) {
            //int total = 0;        
        }
    }


}

I keep getting strange outputs in x. Something like 58. I'm accessing the array backwards and retrieving every 2nd number back to the front of the array. I basically want x to be whatever the number is in each array element. I know with this small piece of code it will overwrite x continuously, but i want it to be the number designated in the char array, not some random number not matching my array elements. The problem is in the calculation method where i try and convert the char to int.

Please help

Thanks.

For Example;

4
  • Note that your "cardArray" is an array of integer values 1,2,3... If you want character values you need '1','2','3',.... Commented May 17, 2013 at 18:24
  • yeah, i should actually explain this, this is just a slightley altered representation of what my code looks like. The way i got those card array elements, was that i extracted each digit from a string using a for loop, using the .charAt(i) method from the string class. It still deosnt work in terms of getting the int x to show what is in the array. Commented May 17, 2013 at 18:29
  • 1
    Note that a "character" in Java (and most other languages) is some form of "Unicode" (for Java UTF16), but which for English can be considered to be ASCII. In ASCII/Unicode the character zero has a numeric value of 48 decimal, 30 hex. In ASCII/Unicode (but not in some other older code standards) the numeric digits are contiguous, with "1" being 49 decimal, "2" being 50 decimal, and "9" being 57 decimal. So you can get the "value" of a character digit by subtracting "0" (decimal 48) from the character's numeric value. Commented May 17, 2013 at 18:34
  • this is the actual code, rather then the less complex one previously Commented May 17, 2013 at 18:34

3 Answers 3

8

You can do

(int)(cardArray[i] - '0')

to convert a single digit to a number.

The reason you can do this is because each char is a number.

'0' = 48
'1' = 49
'2' = 50
'3' = 51
   ...
'9' = 57

Subtraction the value for 0 gets you the integer value for each of the characters.

Sign up to request clarification or add additional context in comments.

Comments

2

chars are actually numbers that represent chars. if you want to convert it into an int, subtract the 0 char

int x = (int) (cardArray[i] - '0');

1 Comment

Except that cardArray doesn't contain characters.
0

You could use Character.getNumericValue(char), See JavaDoc here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.