0

its only my second program with java and im running into some issues. I'm trying to get input from a user, either yes or no, then based on that go to an if else statemene. Heres what I have so far

 String answer= UI.askString("Do you want to continue?");

   if(answer=="yes"){
       UI.println("Lets go");
   }
   else if(answer == "no"){
       UI.println("Thank you. Goodbye");
   }
   else{
       UI.println("Please enter yes or no");
}

Im thinking perhaps its better to use booleans for this? Any help is gladly appreciated!

(also if you're wondering, its a custom import hence the weird syntax in some lines)

Cheers.

1
  • 1
    You should compare strings using .equals() instead of ==. It's also better to use the .equals() method of the literal instead of answer to avoid possible NullPointerExceptions. So if ("yes".equals(answer)). Commented Mar 11, 2014 at 22:28

3 Answers 3

1

When you compare two Strings in Java with the == operator, they are compared to see if they are the same object, rather than whether they contain the same text. So, you could type "yes", and when you use if (answer == "yes") the comparison fails, because the object you got back from UI.askString is a different object, stored at a different place in memory, than the String the compiler generated from the literal "yes".

To compare the value of the two Strings you need to write answer.equals("yes"), or "yes".equals(answer). Either one will work, and will call the equals method of the String class, which will compare the actual text.

The latter syntax, "yes".equals(answer), is often recommended because it will not cause a NullPointerException, even if the variable answer is set to null. This is because the equals method handles null and simply returns false. If, on the other hand, you used the answer.equals("yes") form, and answer was null, you would be trying to invoke a method on null and an exception would be thrown.

Sign up to request clarification or add additional context in comments.

Comments

0

what you are looking for is a dialog box. Here is oracle examples, with code. It is more than I can write here. There are ton of yes, no boxes and detection's of user input with them.

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Quick answer:

int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){ ... }

Other choices ...

 YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, and CLOSED_OPTION

For a command line program you need...

import java.util.Scanner;

The code will look like ...

Scanner in = new Scanner(System.in);
String line = in.nextLine();

//ask them to write yes, no, whatever

if(line.equal("yes"){ }
else if (line.eqals("no") {}
else {}

4 Comments

Maybe you should also explain the meaning of n
Thanks but im looking much simpler, this is just a CLI program (the UI class just simplifies the system.out.println and a few others) so no need for dialog boxes. Thanks anyway!
@user134613 I updated it for you. Please give thumbs up if this helps, if not let me know why not and I'll update it.
Still stuck on the input part where you commented "ask them to write yes, no, whatever" can you please show me an example of how to do this and what value I should be asking for? sorry for the hastle!
0

using MikeG010590's answer, you can try:

Scanner in = new Scanner(System.in);
    String line;
    System.out.println("you want to continue?");
    Boolean exit = null;
    do {
        line = in.nextLine();
        switch (line) {
            case "yes":
                exit = false;
                break;
            case "no":
                exit = true;
                break;
            default:
                System.out.println("Please enter yes or no");
                break;
        }
    }
    while (exit == null);
    System.out.println(exit ? "Thank you. Goodbye" : "Lets go");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.