I have a scanner running where the user can input either strings or integers. There are only specific characters the user can enter such as a,e,u,r and the number can be anything. The check runs if its a letter but fails if the user enters a number.
String temp = scanner.next();
String[] validToken = {"x","e","u","r","+","-","/","*",};
for (String validToken1 : validToken) {
if (temp.equals(validToken1) || temp.equals("\\d+")) {
tokenCheck = true;
}
}
Setinstead of aString[]will enable you to do this:validToken.contains(temp)and like @iNam said, if you want to check aStringagains a regex usematches(....)