6

I was wondering how I can compare multiple strings in one line. I tried using the || but it doesn't work for booleans or strings. this is what my code is like:

}else if(question != "a" || "b") {
    System.out.println("Sorry that isn't an A or a B");

For those who marked it duplicate, I checked over 200 questions here on stack overflow, and none worked. The one @Chrylis posted actually didn't help. they were just asking about the difference in == and .equals()

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
SaraMaeBee
  • 95
  • 1
  • 1
  • 6

6 Answers6

9

First of all, don't use == for strings. You'll learn why later. You want to compare strings by their contents, not where they are in memory. In rare cases a string of "a" could compare false to another string called "a".

Second, split it up so you are performing boolean logic on the comparison results:

else if(!(question.equals("a") || question.equals("b")) {
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Presumably the OP is asking about an approach that would scale well as the number of strings is increased. – arshajii Nov 20 '13 at 00:52
  • 2
    @arshajii If that's an assumption you wish to make, go ahead. I'm going to leave this with my own assumptions and interpretation however. – nanofarad Nov 20 '13 at 00:54
  • hexafraction, I was hoping this would work, and while it exempted me form errors, but when I put an input of c, it doesnt run the code v.v – SaraMaeBee Nov 20 '13 at 01:02
  • also, I usually don't use ==. I just was not sure how else to do it except for != I am fairly new to Java (while ive been coding for awhile, but i never really went in depth(im still only 13)) i wasnt sure if you could do an if(!(ques..."b")) { – SaraMaeBee Nov 20 '13 at 01:09
  • @hexafraction if i was to upload a picture, could you figure it out possibly? I have not the slightest clue why it wont run correctly – SaraMaeBee Nov 20 '13 at 01:15
  • The cases where two strings `"a"` aren't `==` aren't even rare. It's probably more rare that they _are_ `==`. – yshavit Nov 20 '13 at 01:26
  • @yshavit if i have my code running, but it doesnt continue after accepting the unknown variable (it wont run the code) how can i fix it? – SaraMaeBee Nov 20 '13 at 01:33
  • `}else if(!(question.equals("a") || question.equals("b"))) { System.out.println("Sorry that isn't an A or a B"); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); }` – SaraMaeBee Nov 20 '13 at 01:37
  • What code won't it run? What's the value of `question`? – yshavit Nov 20 '13 at 01:52
  • @ yshavitthe value of question was c, and it wouldnt run the System.out.println("Sorry that isn't an A or a B"); – SaraMaeBee Nov 20 '13 at 11:46
5

You can try using Arrays.asList():

else if (!Arrays.asList("a", "b").contains(question)) {
    ...
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Two things wrong: You can't just specify multiple values with || (or &&) like that. You need to specify both the left side and the right side explicitly each time.

Second, use equals to compare String values, not the == (or in this case !=) operators. == compares two object references to see if they are the same object.

} else if (!("a".equals(question) || "b".equals(question)))

Or an alternative is to make a temporary List and use contains, which might be clearer for longer lists of things to test:

} else if (!Arrays.asList("a", "b").contains(question))
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 1
    Or make your own helper function: `boolean isOneOf(String s, String ... a) { for (String as : a) if (as.equals(s)) return true; return false; }` – Per Lindberg May 20 '14 at 07:56
1
String[] options = {"a", "b"}; // Must be sorted.
if (java.util.Arrays.binarySearch(options, question) < 0) {
  System.out.println("Sorry that isn't an A or a B");
}

Alternatively (assuming your strings don't contain |:

if ("a|b".indexOf(question) == -1) {
  System.out.println("Sorry that isn't an A or a B");
}
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

As an aside, you should use equals for objects not ==

To answer your question, you have to repeat the equals call on both sides of the ||

}else if( ! (question.equals("a") || question.equals("b"))  ) {
dkatzel
  • 31,188
  • 3
  • 63
  • 67
0
}else if( !(question.equals("a") || question.equals("b")) {
    System.out.println("Sorry that isn't an A or a B");

You can't do NOT equals a OR b
You have to do NOT(equals a OR equals b)

Secondly, you are comparing strings with !=, but you should be comparing strings using the .equals(String) method. This has been said millions of times, but: == and != are comparing object references, whereas .equals(String) is comparing String values.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97