-5

So I want to make a code that categorizes words to match it with a phrase. catString is a string inputted by the user. I want it to execute the rest of the code only if this input is fire or smoke.

Here's the line I'm having trouble with:

if(catString = "fire" || "smoke" );
Vivek Vermani
  • 1,934
  • 18
  • 45
dracula90
  • 17
  • 5

5 Answers5

6

You need to repeat the catString =. However, you should not be using == (not =, the assignment operator) to compare strings. Use .equals(). So that would look like:

if(catString.equals("fire") || catString.equals("smoke"));
hichris123
  • 10,145
  • 15
  • 56
  • 70
  • Also equals when doing a comparison should be `==` not `=` in general. The reason you don't use it with a string is because you `==` compares the address in memory instead of if the charters of the string are equal. – ansible Feb 06 '14 at 03:06
  • @ansible That's true, I didn't see that at first glance (that they were using `=`. I edited that into the answer, thanks. – hichris123 Feb 06 '14 at 03:08
1

if (catString.equals("fire") || catString.equals("smoke"))

Note that a single = is NOT a comparison operation. It will assign catString, not what you are trying to do here.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1

Use .equals:

if(catString.equals("fire") || catString.equals("smoke"))
    // do something 

By the way, if you're testing for reference equality, use == not = (which is used in assignment).

Community
  • 1
  • 1
pcnThird
  • 2,362
  • 2
  • 19
  • 33
1

use

 if(catString.equals("fire") || catString.equals("smoke"))
Vivek Vermani
  • 1,934
  • 18
  • 45
0

You can use equalsIgnoreCase so that the user can type it whatever case he wants and still matching the catString.

e.g.

if("fire".equalsIgnoreCase(catString) || "smoke".equalsIgnoreCase(catString))

Note: You must first put the "fire" and "smoke" before the equalsIgnoreCase to avoid NullPointerException.

Gelo
  • 105
  • 10