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;
'1','2','3',....