yhsdygdyusgdysgdsudsd
yhsdygdyusgdysgdsudsd

Reputation: 103

scanner not working properly inside if statement

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());

Upvotes: 0

Views: 1196

Answers (6)

Akash Thakare
Akash Thakare

Reputation: 22972

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);

Upvotes: 5

Chamith Malinda
Chamith Malinda

Reputation: 4529

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());

Upvotes: 1

Pieter VDE
Pieter VDE

Reputation: 2235

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"))
...

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

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
}

Upvotes: 4

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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 

Upvotes: 0

Balduz
Balduz

Reputation: 3570

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")) {
    ...
}

Upvotes: 0

Related Questions