0

I'm having an issue where my program is skipping a prompt for user input. The relevant code is below:

System.out.format("Purchase price is $" + "%.2f%n", sum);
    System.out.println("Proceed to payment? (y/n) ");
    String proceed = keyboard.nextLine();

    while(!proceed.equals("n") && (!proceed.equals("y"))) {
        System.out.println("Invalid response. Try again."); 
        System.out.println("Proceed to payment? (y/n) ");
        proceed = keyboard.nextLine();

    if (proceed.equals("n")) {
        System.out.println("Come back next time, " + name + ".");
        System.exit(0);
    }
    else if (proceed.equals("y")) {`

The output should pause at:

Purchase price is $x.00

Proceed to payment? (y/n)

But instead outputs:

Purchase price is $x.00

Proceed to payment? (y/n)

Invalid response. Try again.

Proceed to payment? (y/n)

Would anyone be able to tell me why its ignoring the first scanner prompt?

EDIT: The code works in isolation, the problem is somewhere else inside the program. When I figure it out I'll report back.

1
  • for some reason, the problem disappeared when i used proceed = keyboard.next() instead of keyboard.nextLine() Commented Apr 7, 2016 at 10:31

1 Answer 1

3

This logic in the while loop condition is wrong

while(!proceed.equals("n") && (!proceed.equals("y"))) {

must be

while(!proceed.equals("n") || (!proceed.equals("y"))) {

if you want to keep your logic with the AND condition then MOVE the payment out of the while so that can work

Example:

while (!proceed.equalsIgnoreCase("n") && (!proceed.equalsIgnoreCase("y"))) {
    System.out.println("Invalid response. Try again....");
    proceed = keyboard.nextLine();
}
if (proceed.equalsIgnoreCase("n")) {
    System.out.println("Come back next time, " + name + ".");
    System.exit(0);

} else if (proceed.equalsIgnoreCase("y")) {
    System.out.println("now we are going to pay, " + name + "." + sum);
    System.exit(0);
}
Sign up to request clarification or add additional context in comments.

4 Comments

See I don't believe that's the case. That || means that proceed could equal "n" because "n" doesn't equal "y" and it would pass that test.
Yeah I tested it, as I said, doing an OR like that means that if the user input is n (and therefore fulfills the right side it will not fulfill the left side and the while loop will end. The point of the while loop is to restrict the user from inputting anything other than y and n.
@jdosha I see the point what you mind... I update my code and tried in my pc, is working, please check if is according to your requirements
Using || (OR) is NOT the way to go (edit should be done). The example provided however is the way to go and using String.equalsIgnoreCase() is a good suggestion in case uppercase characters are provided.

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.