jeudi 5 mars 2015

retrieving character from string.length() , using a FOR statement



I am implementing the Luhn algorithm using a for statement and a string.


My question is, does "j" get assigned the value of the character at that location of the string(the digit), or does it get assigned the value of its numerical location in the string length?


Thank you


*apologize if I am not supposed to paste code directly into this


*EDIT: I am pretty sure it is not actually retrieving the characters but rather only its numerical position in the string, researching char method





if (card.length() < 13 || card.length() > 16)
{
JOptionPane.showMessageDialog(null, "Invalid credit card number");
card = JOptionPane.showInputDialog("Please enter the credit card number.");
num = Long.parseLong(card);
}

for (int j = 0; j < card.length(); j++) {
sum = sum + j*2;
if ( j%2 != 0 ) {
product = j * 1;
sum += product;
}
else {
product = j * 2;
}
if (product > 9) {
product -= 9;
sum += product;
}
}



Aucun commentaire:

Enregistrer un commentaire