1

Question: my code works but I don't get why, should it not be the opposite way? that if entered "Yes" or "No" the System.out.println("You must write 'Yes' or 'No'") should appear in the console?

Please explain for a dummie how/why it works this way.

import java.util.Scanner;

public class YesOrNo {
    public static void main(String[] args) {
        // Checkpoint 4.6 Write an input validation loop that asks the user to enter “Yes” or “No”.
        String Input; 
        Scanner keyboard = new Scanner(System.in); 
        System.out.println("Enter Yes or No: ");
        Input = keyboard.nextLine(); 
        while (!Input.equals ("Yes") && !Input.equals ("No")){
            System.out.println("You must write 'Yes' or 'No'");
        }
    System.exit(0);
    }
}
1
  • 2
    in java we use camelCase for the variables.... Commented Apr 3, 2017 at 14:37

4 Answers 4

1

Your code can/will loop for ever if you dont read the user input again:

Input = keyboard.nextLine(); 
while (!Input.equals ("Yes") && !Input.equals ("No")){
    System.out.println("You must write 'Yes' or 'No'");
    Input = keyboard.nextLine(); 
}

consider too, using equalsIgnoreCase so you have no problem to accept case variant inputs from the user....

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

Comments

1

You don't read input again in your loop, so your input is not changed ever :

while (!Input.equals("Yes") && !Input.equals("No")) {
    System.out.println("You must write 'Yes' or 'No'");
    Input = keyboard.nextLine();//<<---------------------
}

Note

For the good practice don't name your variable with Upper Letter in start (Input)

Edit

Like @Ousmane Mahy Diaw says in comment you can take a look at this Java.lang.String.equalsIgnoreCase()

Comments

0

A while block is executed if the condition is true.

You want to display the message while the input is different from "Yes" and "No".

In java, for the String type, there is no "differents" method, but there is an "equals" one. In the condition, "different" could be replace by "is not equals". So you want to diplay it while the input is not equals to "Yes" or "No".

If we translate the condition to java, you get:

!(input.equals("Yes") || input.equals("No"))

Boolean algreba says that !(A||B) equals (!A && !B).

So you can convert the previous line to

!input.equals("Yes") && !input.equals("No")

That should answer your question about why your code works.

Now there still is an issue in your code. If the user write something different from Yes or No, the message will keep getting displayed and the user will have no way to input a new value. You have to add code to ask the user a new value each time the loop is executed:

while (!input.equals("Yes") && !input.equals("No")){
    System.out.println("You must write 'Yes' or 'No'");
    input = keyboard.nextLine();
}

Note: In java, we use camelCase for the variable names, that why I wrote input instead if Input

1 Comment

Thx! I understand now :) I've modified the code a bit according to all comments
0

Thx! I've modified the code now, works better and I finaly understand why :)

import java.util.Scanner;

public class YesOrNo {
   public static void main(String[] args) {
        String input; 
        Scanner keyboard = new Scanner(System.in); 
        System.out.println("Enter Yes or No: ");
        input = keyboard.nextLine(); 
        while (!input.equals ("Yes") && !input.equals ("No")){
            System.out.println("You must write 'Yes' or 'No'");
            input = keyboard.nextLine();
        }
        while (input.equals ("Yes") || input.equals ("No")){
            System.out.println("Thx");
            break;
        }
    System.exit(0); 
    }
}

1 Comment

The second while loop is useless. You ask the programm to loop on a block that ask to stop looping (that what the break instruction does.).you can replace this whole loop just with the system.out.println("thx");as it will be executed only once the previous loop block ends.

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.