class LuhnValidator {
boolean isValid(String candidate) {
int totalsum = 0;
candidate = candidate.replace(" ", "");
if(candidate.length() <=1) return false;
for(int n = candidate.length()-1; n >= 0; n--){
if(!Character.isDigit(candidate.charAt(n))) return false;
if(candidate.length() % 2==0){
if(n % 2 ==0){
int newCandidate = Character.getNumericValue(candidate.charAt(n));
newCandidate *=2;
if(newCandidate > 9) newCandidate -=9;
totalsum += newCandidate;
}
else{
int newCandidateEven = Character.getNumericValue(candidate.charAt(n));
totalsum += newCandidateEven;
}
}
else{
if(n % 2 !=0){
int newCandidate = Character.getNumericValue(candidate.charAt(n));
newCandidate *=2;
if(newCandidate > 9) newCandidate -=9;
totalsum += newCandidate;
}
else{
int newCandidateEven = Character.getNumericValue(candidate.charAt(n));
totalsum += newCandidateEven;
}
}
}
return totalsum % 10==0;
}
}