-4

So, for some reason I'm having problems just USING a string input.

I don't know why. Maybe it's some incredibly stupid thing everyone knows, but I don't.

Here's the non-functioning code:

import javax.swing.*;

public class Thing {
public static void main(String[] args) {
    String input;

    JOptionPane.showMessageDialog(null,"Welcome to the test...");
    input = JOptionPane.showInputDialog("Do you wish to take the tutorial?" + "\n" +
                                        "If affirmative, enter 'Yes'");
    String i = input;

    if(i == "Yes") {
        tutorial();
    } else if(input=="'Yes'") {
        JOptionPane.showMessageDialog(null,"Don't actually put apostraphes around you're answer.");
        tutorial();
    } else {
        JOptionPane.showMessageDialog(null,"Remember, you can pull up the tutorial at any time with 'T'");
    }
}

Yes, I actually do have a tutorial method somewhere else, and it works fine.

The main problem is that if I enter 'Yes' or Yes, it still goes to the final else.

I only put in the

String i = input;

and changed it from

if(input == "Yes") {

because it didn't work then, either.

So what am I doing wrong?

rgettman
  • 176,041
  • 30
  • 275
  • 357

2 Answers2

4

Don't use the == operator to compare Strings, use equals() instead, as thoroughly explained here, here, here, here or any of the numerous duplicates.

if ("Yes".equals(input))

Or even

if ("yes".equalsIgnoreCase(input))

Notice that the operation is invoked on the "yes" literal to avoid a possible NullPointerException in the case input was null and the operation was invoked on it (Yoda condition).

From the Java Language Specification, Chapter 15 - Expressions, section 21 - Equality Operators:

15.21.3. Reference Equality Operators == and !=

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
1

As mentioned, the problem is that you are comparing this String using the == comparator, not the .equals() method.

If you are running on Java 7, my advice, for a cleaner solution, would be also to wrap this in a switch statement:

    JOptionPane.showMessageDialog(null,"Welcome to the test...");
    String input = JOptionPane.showInputDialog("Do you wish to take the tutorial?" + "\n" +
                                        "If affirmative, enter 'Yes'");
    switch (input) {
        case "Yes":
            tutorial();
            break;

        case "'Yes'":
            JOptionPane.showMessageDialog(null,"Don't actually put apostraphes around you're answer.");
            tutorial();
                    break;

        default:
            JOptionPane.showMessageDialog(null,"Remember, you can pull up the tutorial at any time with 'T'");
    }
everton
  • 7,579
  • 2
  • 29
  • 42