Operator || undefined for argument type (s) boolean, String

I keep getting the above error message with the following IF statement. Any help is appreciated.

public void sendMessage(View button) { String mName = Name.getText().toString(); 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(); } 
+4
source share
4 answers

First, you should not use != To compare strings; use equals() instead. The == and != Operators will only check if strings are the same objects; they do not check for equal values. Secondly, you need to expand the expression as follows:

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

Finally, this logic does not really make any sense. The condition will always be true ( mGuess will always be "not equal" for at least all but one of the test lines). You probably want:

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

A more concise way to do this:

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

(You can declare validStrings as a member of the static class to save it each time using the method. Also see assylias answer on how to use a HashSet instead of an ArrayList to search, will do the search faster.)

PS As assylias as well as kcoppock was mentioned in the comment, you should consider parsing the input as an int value and then do a numerical test. The difference would be that parsing as an int would handle, say, “07” in the same way as “7”. If you want to allow this, then this code will complete the task:

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

You need to make each condition explicit, as already explained. More compact way to record:

 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 can parse it into an integer first:

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

The reason for compiler 1 error is that the expression

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

analyzed equivalently

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

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

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

but boolean || String boolean || String is invalid, as in the case of a compiler error:

Operator || undefined for argument type (s) boolean, String


1 See one of the other answers for solutions.

+6
source

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

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

See also: Java String.equals versus ==

+2
source

All Articles