I am trying to check IMEI number using Stream API help in Java 8.
private void ValidateIMEI() {
field
.getText().chars()
.map(this::ConvertASCIIToNumer);
}
The part I'm stuck with is doubling an even number and dividing by 10.
First I tried the traditional loop:
private void ValidateIMEI() {
int[] numbers = field
.getText().chars()
.map(this::ConvertASCIIToNumer).toArray();
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
if ((numbers[i]+1) % 2 == 0) {
numbers[i] = numbers[i] * 2;
numbers[i] = numbers[i] / 10 + numbers[i] % 10;
}
sum += numbers[i];
}
if (sum%10==0) {
status.setText("Valid");
}
else{
status.setText("InValid");
}
}
But the code is broken and most specifically uses a For loop, which I don't want.
So, can anyone help implement the Luhn algorithm using only the Stream API in Java 8?
Code for ConvertASCIIToNumer:
private int ConvertASCIIToNumer(int value) {
return Character.digit(value, 10);
}
source
share