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") || ) { . . .
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") && ) { . . .
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) { . . .
source share