0
Scanner input = new Scanner(System.in);
System.out.println("You are in a small room.");
String op1 = input.nextLine();
    if(op1 == ("look at surroundings" != null || "surroundings" != null || "look at surroundings" !=null || "what does the room look like" != null)) {

}

It returns with this error Incompatible operand types String and boolean I am a very inexperienced java programmer. I have looked all over for an answer, but couldn't find one.

  • i guess you are trying to compare string with boolean – upog Oct 02 '13 at 16:29
  • You need to use String's methods of equals or equalsIgnoreCase. Otherwise you are testing object equality, which in this case, will never eval to true. – Eric Oct 02 '13 at 16:30
  • You should perhaps explain what you are trying to do. It is not really clear from your code. – Rohit Jain Oct 02 '13 at 16:30

6 Answers6

2

You have 2 problems :

1)

if(op1 ==

Never use == on String. use String.equals();.

2)

 ("look at surroundings" != null || [...] )

is of type boolean

So you cannot compare it to op1 which is a String

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
2

Basically, you need to use the String.equals or String.equalsIgnoreCase comparisons instead. For a text game with variable user input (if they insist on having caps-lock on, for example), I'd recommend using equalsIgnoreCase. As upog suggested, also make sure that the user isn't prepending or appending unneeded spaces to the end of the input. Try this instead:

Scanner input = new Scanner(System.in);

System.out.println("You are in a small room.");
String op1 = input.nextLine();
op1 = (op1 == null ? "" : op1.trim());

if("look at surroundings".equalsIgnoreCase(op1)
    || "surroundings".equalsIgnoreCase(op1)
    || "what does the room look like".equalsIgnoreCase(op1))
{
    // Look around
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
  • 3
    you have to use equals – upog Oct 02 '13 at 16:31
  • I did one better @upog: equalsIgnoreCase. Granted, I literally _just_ edited it when you posted your comment. – Chris Forrence Oct 02 '13 at 16:32
  • you can also add trim (), to make it even better – upog Oct 02 '13 at 16:33
  • @upog: Added ternary setter for op1 to trim it if not null (and to set it to a blank string if null, which would allow for the cleaner-looking `op1.equalsIgnoreCase("...")` format) – Chris Forrence Oct 02 '13 at 16:37
  • if op1 is null, i prefer to return (or) skip the if condition, instead of assiging "" and executing the if conditon – upog Oct 02 '13 at 16:39
  • @upog: It actually shouldn't be null, since I don't see anywhere in the [the documentation](http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextLine()) that it can be null. – Chris Forrence Oct 02 '13 at 16:44
  • So i guess then you can remove the null check and retain trim() – upog Oct 02 '13 at 16:44
  • Thank you a lot. This is my first time asking stack overflow for help. This is very helpful and well documented. Thank you again. – user2839482 Oct 02 '13 at 18:32
1

try this

Scanner input = new Scanner(System.in);
System.out.println("You are in a small room.");
String op1 = input.nextLine();
    if(op1.equals("look at surroundings") || op1.equals("surroundings") || op1.equals("look at surroundings")||op1.equals("what does the room look like")) {

}
Jean Marcos
  • 167
  • 6
1

Background: I used to program on the 3 Kingdoms MUD.

You are going to rapidly find that you don't want to use this kind of logic for command loop handling. I suggest that you look at using a Map<string, IGameCommand> - where IGameCommand provides the actual doing-work part.

Usage would look like this:

// might be other things you want in this interface later, as well...
interface IGameCommand
{
    void Invoke(string commandline);
}

if (myMap.containsKey(op1))
{ 
    myMap[op1].Invoke(op1);
}

This method is much easier to read and lets you merge a global dictionary of commands with commands added by the location and carried items of the player MUCH more easily. (Yes, this is a bit deeper into Java than you seem to be. When you're ready to use the tools I'm recommending here, you'll find you're starting to get good at Java. When you're ready to argue about what I've glossed over.... you'll be answering people's questions on SO.)

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
0

Besides using .equals() on strings

if ("look at surroundings".equals(ip)) { ...

(beware of null strings, mind, hence the order of comparison), you may find Java 7's switch statement useful. See here for more details. Note that I say Java 7 since the switch statement was amended to work with strings specifically only since Java 7.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

I think you want something like this:

if(op1.equals("look at surroundings") || op1.equals("surroundings") || op1.equals("look at surroundings") || op1.equals("what does the room look like")) 
    {
     // your code
     }

Be aware that string comparasion is done by using String.equals() or String.equalsIgnoreCase() methods.

Fabio
  • 791
  • 1
  • 7
  • 27