I tried to check the validation of credit card using Luhn algorithm, which works as the following steps:
Double every second digit from right to left. If do
There are two ways to split up your int into List
%10 as you are using and store it into a ListString and then take the numeric valuesHere are a couple of quick examples
public static void main(String[] args) throws Exception {
final int num = 12345;
final List nums1 = splitInt(num);
final List nums2 = splitString(num);
System.out.println(nums1);
System.out.println(nums2);
}
private static List splitInt(int num) {
final List ints = new ArrayList<>();
while (num > 0) {
ints.add(0, num % 10);
num /= 10;
}
return ints;
}
private static List splitString(int num) {
final List ints = new ArrayList<>();
for (final char c : Integer.toString(num).toCharArray()) {
ints.add(Character.getNumericValue(c));
}
return ints;
}