Ciaran
Ciaran

Reputation: 555

How can I test a string against multiple others in Java?

The operator || is undefined for the argument type(s) boolean, String

I keep getting the above error message with the following if statement:

String mGuess = Guess.getText().toString();
if (mGuess != "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9" || "10") {
    Toast.makeText(MainActivity.this,
        "The number you entered was invalid. Please try again.",
        Toast.LENGTH_LONG).show();
}

Upvotes: 4

Views: 13457

Answers (4)

user166390
user166390

Reputation:

The cause of the compiler error1 is that the expression

mGuess != "1" || "2" || ..

is parsed equivalently to

((mGuess != "1") || "2") || ..

However, the type of myGuess != "1" is boolean, so the above expression is typed as

((boolean) || String) || String) || ..

but boolean || String is invalid, as per the compiler error:

The operator || is undefined for the argument type(s) boolean, String


1 See one of the other answers for solutions.

Upvotes: 6

Ted Hopp
Ted Hopp

Reputation: 234857

First, you should generally not be using != to compare strings; use equals() instead. The == and != operators will only test if the strings are identical objects; they do not test for equal values. Second, you need to expand the expression like this:

if (!mGuess.equals("1") || !mGuess.equals("2") || /* etc */) { . . .

Finally, this logic doesn't actually make any sense. The condition will always be true (mGuess will always be "not equal" to at least all but one of the test strings). You probably want:

if (!mGuess.equals("1") && !mGuess.equals("2") && /* etc */) { . . .

A more succinct way of doing this would be:

List<String> validStrings = Arrays.asList("1", "2", ...);
if (!validStrings.contains(mGuess)) { ...

(You could declare validStrings as a static class member to save creating one each time through the method. Also, see the answer by assylias for how to use a HashSet instead of an ArrayList for the lookup; it will do the lookup faster.)

P.S. As mentioned by assylias and also by kcoppock in a comment, you should consider parsing the input as an int value and then doing a numeric test. The difference would be that parsing as an int would treat, say, "07" the same as "7". If you want to allow that, then this code will do the job:

boolean ok = false;
try {
    int guess = Integer.parseInt(mGuess);
    ok = guess >= 1 && guess <= 10;
} catch (NumberFormatException ignored) {
}
if (!ok) { . . .

Upvotes: 13

assylias
assylias

Reputation: 328913

You need to make each condition explicit as already explained. A more compact way to write it would be:

Set<String> oneToTen = new HashSet<String> (Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");

if (!oneToTen.contains(mGuess)) {

Alternatively, if you know that mGuess is a number, you could parse it to an integer first:

int guess = Integer.parseInt(mGuess);
if (guess < 0 || guess > 10) {
}

Upvotes: 6

Reimeus
Reimeus

Reputation: 159874

You need to use && to evaluate your negative expressions, use .equals for String comparisons, and have syntactically correct expressions in your if statement:

if (!mGuess.equals("1") && !mGuess.equals("2") && ...

Also see: Java String.equals versus ==

Upvotes: 2

Related Questions