Cheese Cheese - 1 year ago 147
Java Question

Java scanner check if string or int

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 =;
String[] validToken = {"x","e","u","r","+","-","/","*",};
for (String validToken1 : validToken) {
if (temp.equals(validToken1) || temp.equals("\\d+")) {
tokenCheck = true;

Answer Source

Change equals to matches. matches is used to check whether or not string matches a particular regular expression.

if (temp.equals(validToken1) || temp.matches("\\d+")) {
                     tokenCheck = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download