0

I was am trying to create a simple text based randomly generated fighting game, and have create the game logic(ish). Now i would like to receive input using TextIO. I know how to receive one char, boolean, int and double. I have tried to use this (missing all other game logic), but it doesn't work

String input = null;

input = TextIO.getlnString();

    if (input == "help"){

    System.out.println("You are currently in "+ sTerrain + " terrain.\nAre you fighting: " + sFighting
        + "\nAre you alive: " + sLife + "\nAre there any NPC's: " + sNpc
        + "\nWhich type of NPCs: " + npc + "\nYour health is: " + currHealth + "/" + health);

        }   // Closes off help

If anyone has some expertise with TextIO, and could help me with allowing multiple outputs for different inputs, using the same String, it would be apprecaited

-Jordan

zx81
  • 41,100
  • 9
  • 89
  • 105
Jordan.McBride
  • 267
  • 2
  • 7
  • 20
  • possible duplicate [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reimeus Oct 08 '13 at 00:48

1 Answers1

0

Use String equals method for string comparison instead of ==. String equals method check whether the content of two string are same whereas == checks whether two references are pointing to the same memory location or not. Change this:

if (input == "help"){

to

if ("help".equals(input))

Also note the reverse check using string literal, this helps to avoid NullPointerException in case input is null.

Check this related post: Java String.equals versus ==

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Sorry, about asking a duplicate question, i tried searching the forums, but as i didn't know what to search for i missed those ones. Sorry – Jordan.McBride Oct 08 '13 at 00:55