Search code examples
javaif-statementcharat

Is there a cleaner way of doing this if statement?


if(input.charAt(i) == '0' || input.charAt(i) == '1' || input.charAt(i) == '2') {
}

Is there a way to condense this if condition, or no?


Solution

  • You could check if the character matches any index in a common String. Like,

    if ("012".indexOf(input.charAt(i)) > -1) {
    
    }