2
   import java.util.Scanner;

   //this program test input validation yes or no program

public class Fool
 {
   public static void main(String [] args)
   {
    String input;
    char first;
    Scanner keyboard=new Scanner(System.in);

    System.out.println("Enter yes or no ");
         input=keyboard.nextLine();
        first=input.charAt(0);
        System.out.print(first);
         while( first !='y' || first !='n')
          {
        System.out.println("please enter yes or no");
          }

       }
   }

What is trying to get the program to is that the user has to remain in the while loop if the user does not put in yes or no.

2
  • Think about that while condition logically, it will always be true. Commented Dec 20, 2014 at 3:13
  • The while should contain input=keyboard.nextLine();. Otherwise, first stays the same forever. Commented Dec 20, 2014 at 3:28

4 Answers 4

2

change this to

while( first !='y' || first !='n') {
        System.out.println("please enter yes or no");
}

this

while( first !='y' && first !='n') {
        System.out.println("please enter yes or no");
}

because (first !='y' || first !='n') is always true.

if first =='y' then first !='n' is true

if first =='n' then first !='y' is true.

so while condition is always true what you need is not || but && [and ]

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

3 Comments

Huh I though the logic would while first does not equal y or first does not equal n, oh I see the or statement needs only one statement to be true so if someone puts in no, then first will not equal 'y' and thus the loop will start.
Maybe this helps or maybe confuses you more: while( first !='y' && first !='n') is equivalent to while(!(first =='y' || first =='n')) and I find the latter more readable as it's more like how you describe the condition in English.
Yes that is Demorgans logicc law
1
while( first !='y' || first !='n') is always true. 

Replace your code with:

while( first !='y' && first !='n')

Comments

1

while( first !='y' || first !='n') is always true. As OR operation works as follows

condition 1 condition 2 result

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE

In your case one condition will be always true, therefore it enters in while loop everytime While AND operation works as follows

condition 1 condition 2 result

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE

so instead of using OR try using AND e.g. while( first !='y' && first !='n')

Comments

1

You should change your code to

boolean b=false;
while(b==false){
    if(first !='y' || first !='n'){
        System.out.println("please enter yes or no");
    } else {
        b=true;
    }
}

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.