-1

I was working on my game I am making, when I came across an error. My if/else if statement skips right to the else if statement, even if it shouldn't.

String neededCredits = "200";

 if(Peeamnt.getText() == neededCredits) {
        System.out.println("You can afford this");
JOptionPane.showMessageDialog(BuyPoopButton,
        "You have unlocked the Poop Button for 200 Pee Credits!",
        "Toilet Master",
        JOptionPane.WARNING_MESSAGE);
}
    else if((!(Peeamnt.getText() == neededCredits))) {
        System.out.println("You cannot afford this");
        JOptionPane.showMessageDialog(BuyPoopButton,
                "You do not have enough Credits to buy this!\n"
                + "To buy it, you need 200 Pee Credits!",
                "Toilet Master",
                JOptionPane.ERROR_MESSAGE);
}

Even if the text of Peeamnt is an even 200, the code will jump to the else if statement, telling me that I don't have 200 Pee Credits. (The game I am making included a lot of toilet humor.) Anyway, if anyone sees the error I have in this code, please let me know. Let me know if you need more code.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Partly. I guess .equals() { fixed my problem. Thanks. ;) – FreshCoffee Sep 11 '13 at 01:13
  • There's probably no need to call `getText` twice. If `neededCredits.equals(Peeamnt.getText())` is false, then the `else` part will automatically happen. You don't need to put the `neededCredits` test in there again. And if `getText()` actually does something like wait for the user to type something in, then the code you posted will wait for input twice which is probably not what you want. – ajb Sep 11 '13 at 01:36

6 Answers6

2

With a Java String object, the == operator doesn't compare the string value.
Try changing the first if comparison to:

if(Peeamnt.getText().equals(neededCredits)) {

You will need to do something similar for the else if as well.

Scott Leis
  • 2,810
  • 6
  • 28
  • 42
1

Strings are objects. Objects have a reference. Two String objects containing the same sequence of characters may not be the same object, thus having different references. The == operator (generally) checks for reference equality.

To compare the character sequence of two String objects for equality, you have the equals method. So use Peeamnt.getText().equals(neededCredits) instead.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
1

String is an Object. Comparing Object, you have to use equals to judge whether the Object content is same. Using == is to compare Object reference

yushulx
  • 11,695
  • 8
  • 37
  • 64
1

Use equals method to compare String object, because == operator means you compare object base on memory address. Always remember to never use == to compare objects in Java.

princepiero
  • 1,287
  • 3
  • 15
  • 28
0

Try to use equals method if the getText() returns a string don 't use == sign. I suppose that getText() returns aString` object.

FYI: Double equal sign is used to see if two Objects are the same and TO check if the objects has the same value equals() method should be used. Note: The objects that You compare with equals() should override it otherwise the results are corrupted, and the last thing when You overridesequals() method remember to override hashCode() too.

LeandreM
  • 943
  • 3
  • 12
  • 24
0

Use method equals to compare String object, because == operator means you compare object base on memory address. * never use == to compare object.

String neededCredits = "200";

 if(neededCredits.equals(Peeamnt.getText()) {//compare following you never see, because  nullPointerException "neededCredits" always has value :-)
        System.out.println("You can afford this");
JOptionPane.showMessageDialog(BuyPoopButton,
        "You have unlocked the Poop Button for 200 Pee Credits!",
        "Toilet Master",
        JOptionPane.WARNING_MESSAGE);
}
    else if((!(neededCredits.equals(Peeamnt.getText()))) {
        System.out.println("You cannot afford this");
        JOptionPane.showMessageDialog(BuyPoopButton,
                "You do not have enough Credits to buy this!\n"
                + "To buy it, you need 200 Pee Credits!",
                "Toilet Master",
                JOptionPane.ERROR_MESSAGE);
}
zerone
  • 105
  • 1
  • 3
  • 10