1

I've Tried Just About Everything and I still can't this dang code to run properly

public void Action(ActionEvent event)
{
    //We get the text from the textfield
    String fromUser = textfield1.getText();

    if (fromUser != null) {
        if(x<1)
        {
            if(fromUser.length() <= 10)
            {                                
                USERNAME = fromUser;
                x++;
                textfield1.setText("");
            }
            else
            {
                textarea1.setText("Username Surpasses Size Limit Please Try Again\nUsername Must Be 10 Characters Or Less");
                textfield1.setText("");
            }
        }
        else
        {
            if(x==1)
            {
                textarea1.setText("");
                x++;
            }
            //We append the text from the user
            textarea1.append(USERNAME+"> " + fromUser + "\n");
            //The pane auto-scrolls with each new response added
            textarea1.setCaretPosition(textarea1.getDocument().getLength());
            //We reset our text field to "" each time the user presses Enter
            textfield1.setText("");

            //This is Where my code should work
            if(fromUser == "n")
            {
                InitiateText("North");
            }
            else if(fromUser == "s")
            {
                InitiateText("South");
            }
            else if(fromUser == "w")
            {
                InitiateText("West");
            }
            else if(fromUser == "e")
            {
                InitiateText("East");
            }
            else
            {
                InitiateText("I don't comprehend");
            }
        }
    }
}   

public void InitiateText(String Where) //Where are you on the map
{
    //We append the text from the user
    textarea1.append("Map > " + Where + "\n");
    //The pane auto-scrolls with each new response added
    textarea1.setCaretPosition(textarea1.getDocument().getLength());
    //We reset our text field to "" each time the user presses Enter
    textfield1.setText("");
}

This was my latest attempt to do something, the method of adding the text works fine but the if statements won't work, i'll input "n" and it will not recognize the "n" I've inputted. What am I doing wrong??

mKorbel
  • 109,525
  • 20
  • 134
  • 319
The UFO Files
  • 11
  • 1
  • 2

1 Answers1

4

Change if(fromUser == "n") to if(fromUser.equals("n")) and change every statement to compare strings using equals() method instead of ==.

== operator simply checks whether references refer to same string objects, they do not check for equality of String. Read String comparison with equals and assignment operator

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56