0

This code prompts the user to confirm a booking by typing Y or N (yes/no). If they type y or Y it calls a method setBooked() which basically just sets a boolean variable "booked" to "true". The isBooked() just returns that boolean value so I could test the before/after to see if it actually worked.

The actual code does work just not as I expected, it will immediately work properly if you type "y" but if you type anything else it will prompt you again, and again work if you type "y" but this time if you type anything else it just stops and moves on to the next "customer" (method is called about 8 times)

So basically is there a reason that it is prompting the user twice instead of just evaluating what they type the first time for "y" OR "Y"?

System.out.println(customer.isBooked());
System.out.println( "Confirm booking for " + customer.getName() + "(Y/N)");
Scanner scan = new Scanner(System.in);

if (scan.nextLine().equals("y") || scan.nextLine().equals("Y"))
    customer.setBooked();

System.out.println("Booked");
System.out.println(customer.isBooked());
0

6 Answers 6

5

You should use #equalsIgnoreCase

Use scan.nextLine().equalsIgnoreCase("y") instead, as || will go and check both the conditions as you will be prompted for nextLine() twice.

If you want user to keep asking for input if user enters wrong input you should use loop and prompt until condition gets satisfied.

For Example

     do {
         System.out.println("Type 'y' OR 'Y' to Exit!");
         if(s.nextLine().equalsIgnoreCase("y")) {
            customer.setBooked();
            break;
         }
      } while(true);
Sign up to request clarification or add additional context in comments.

1 Comment

ahh. equalsIgnoreCase() damn it ~~ :P
4

It is prompting twice, because you are asking it to prompt twice.

here : if (scan.nextLine().equals("y") || scan.nextLine().equals("Y")) You are calling scan.nextLine() two times.

Change your code to :

String s = scan.nextLine();
s=s.toLowerCase(); // change "Y" to "y" . Cleaner code.

if(s.equals("y")){
//your code here
}

Comments

1

Try using following code:

System.out.println(customer.isBooked());
System.out.println( "Confirm booking for " + customer.getName() + "(Y/N)");
Scanner scan = new Scanner(System.in);
boolean flag = scan.nextLine().equalsIgnoreCase("y");
if (flag)
    customer.setBooked();
System.out.println("Booked");
System.out.println(customer.isBooked());

Comments

0

You are calling scan.nextLine() twice in the condition, using an OR. This means that if the left side is not true, then it will continue to the right side. However if it is true, then the OR will be true and does not need to evaluate the right side. That is why if when they enter y the first time it only asks for it once, but otherwise it asks for it twice.

If you only want it to ask for it once, then instead of doing that assign the value of scan.nextLine() to a variable and use that variable in the if statement.

String result = scan.nextLine();
if (result.equals("y") || result.equals("Y")) {
    ...
}

Comments

0

Yes, You are calling two times

 scan.nextLine().equals("y") || scan.nextLine().equals("Y") //2 scan.nextLine()

Change your code to

 String line=scan.nextLine();
 if ("y".equals(line) ||"Y".equals(line)) // avoid NullPointerException too

Or use equalsIgnoreCase()

 if("y".equalsIgnoreCase(scan.nextLine())) // avoid NullPointerException too 

1 Comment

I doubt that call of Scanner#nextLine throws a NullPointerException unless the underlying input stream breaks abruptly but this would mean there's an external cause that will break the application.
0

You are stating it should prompt twice here:

if (scan.nextLine().equals("y") || scan.nextLine().equals("Y"))

by calling scan.nextLine() twice.

What you could do is the following:

...
String next = scan.nextLine();    

if (next.equals("y") || next.equals("Y"))
...

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.