Here's the code:
System.out.println("Qualifies for instate rate: ");
instate = keyboard.nextLine();
while((instate.equalsIgnoreCase("yes") == false || instate.equalsIgnoreCase("no") == false))
{
System.out.println("Enter either yes or no:");
instate = keyboard.nextLine();
}
My problem is the output is never-ending; here's the output
Enter either yes or no:
no
Enter either yes or no:
no
Enter either yes or no:
yes
Enter either yes or no:
yup
Enter either yes or no:
It doesn't even matter what I enter in the keyboard.
Please tell me the problem and possible solutions.
Change:
System.out.println("Qualifies for instate rate: ");
instate = keyboard.nextLine();
while((instate.equalsIgnoreCase("yes") == false || instate.equalsIgnoreCase("no") == false))
{
System.out.println("Enter either yes or no:");
instate = keyboard.nextLine();
}
To:
System.out.println("Qualifies for instate rate: ");
instate = keyboard.nextLine();
while((instate.equalsIgnoreCase("yes") == false && instate.equalsIgnoreCase("no") == false))
{
System.out.println("Enter either yes or no:");
instate = keyboard.nextLine();
}
Change your condition inside the while loop to be :
(instate.equalsIgnoreCase("yes") == false && instate.equalsIgnoreCase("no") == false)
It because of DeMorgan's Rule in Boolean Algebra:
!A || !B = !(A && B)
!A && !B = !(A || B)
Change your condition to the following:
!Arrays.asList(new String[]{"yes", "no"}).contains(instate.toLowerCase())
this line instate.equalsIgnoreCase("yes") already returns a boolean so you don't need to compare it again.. try this.
while((!instate.equalsIgnoreCase("yes")) && (!instate.equalsIgnoreCase("no")))
One of the boolean expression in the while condition will always be true. "instate" cannot be "yes" and "no" at the same time. If you like to terminate the loop on "no" input, try
while (!instate.equalsIgnoreCase("no")) {
}
In your while loop condition:
while((instate.equalsIgnoreCase("yes") == false || instate.equalsIgnoreCase("no") == false))
{
System.out.println("Enter either yes or no:");
instate = keyboard.nextLine();
}
You have it testing if either "yes" is not the input or "no" is not the input, so if they type "yes", it will see that they didn't type "no", then repeat the loop, and if they typed "no", it will see that yes was not typed and repeat again. Change the || operator to the && operator so it makes sure that if either "yes" or "no" is typed, and if neither is typed, then it will repeat.
while((instate.equalsIgnoreCase("yes") == false && instate.equalsIgnoreCase("no") == false))
{
System.out.println("Enter either yes or no:");
instate = keyboard.nextLine();
}
Related
Here is the code;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Do you need instructions for this game? Y/N.");
char a = input.next().charAt(0);
// This while loop always comes out as true.
while (a != 'y' || a != 'n') {
System.out.println("Please enter either Y/N. ");
System.exit(0);
}
if (a == 'y') {
System.out.println("This game is called heads and tails.");
System.out.println("You must type h (heads), or t (tails).");
System.out.println("You will need to keep guessing correctly, each correct guess will increase your score.");
}
}
}
Is there an explanation on why it always comes out as true, and is there an alternative way of doing this? I want to have a validation check, where if the user inputs anything other than y, or n, the program shuts down.
The problem is, when I enter the character, y, or n, it shuts down anyway even though I'm using the != (not equals) operator.
If you have a==y, then a != 'n' is true and a != 'y' || a != 'n' is true.
If you have a==n, then a != 'y' is true and a != 'y' || a != 'n' is true.
If you have a == other thing, a != 'y' || a != 'n' is true.
It is everytime true with the OR operation. Need use AND.
(a != 'y' || a != 'n') at least one of the sub-conditions must be true.
Consider the three possible cases:
a is 'y': false || true gives true
a is 'n': true || false gives true
a is something else: true || true gives true
The character a cannot both be y and n, so the while loop is executed for any input.
Besides, the loop is not looping.
You're checking whether a is not equal to 'y' OR a is not equal to 'n'.
This is always true.
Change it into while ((a != 'y') && (a != 'n')).
The condition inside while in
while (a != 'y' || a != 'n')
is always true because
if a is equal to y, then a is obviously not equal to n. So, result is true.
And again, if a is equal to n, then a is obviously not equal to y. So, result is true.
And again, if a is not equal to y or n, then also the result is true.
So, the condition inside the while is always true. And for this reason, the execution is entering the while loop and after printing your message it is exiting.
So using AND instead of OR may solve your problem, like
while(a != 'y' && a !='n') {
//your work
}
And I think you willing to do this like below,
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Do you need instructions for this game? Y/N: ");
char a = input.next().charAt(0);
while (a != 'y') {
if(a =='n') {
System.exit(0);
}
else{
System.out.println("Please enter either Y/N : ");
a = input.next().charAt(0);
}
}
if (a == 'y') {
System.out.println("This game is called heads and tails.");
System.out.println("You must type h (heads), or t (tails).");
System.out.println("You will need to keep guessing correctly, each correct guess will increase your score.");
}
}
}
Your logic should be "a is not y and a is not n"
while (a != 'y' && a != 'n')
do {
System.out.println("Set the A param: ");
if(input.hasNextDouble() == true) {
A = input.nextDouble();
if(A == 0) {
System.out.println("Param A cannot be a 0!");
}
} else if(input.hasNextDouble() == false) {
System.out.println("Param A must be a number!");
}
} while(A == 0 || input.hasNextDouble() == false);
Hello, I'm really new in Java and I found an obstacle I can't resolve by myself.
Everything is okay until I enter some letter instead of number, then this do..while loop keeps repeating itself.
After some search I suppose this might be a problem with a Scanner buffer becouse I should clear it before every loop with input.nextLine() but I don't really know where in code should I put it.
Thanks for any help.
You only actually consume data from the scanner if input.hasNextDouble() is true.
Currently, if A == 0 and there are non-numeric data in the buffer then you'll indeed loop indefinitely.
You need to consume data from the buffer on all control paths. In particular, if there is something non-numeric in the buffer, then you need to consume and immediately discard it: input.next(); would be adequate.
Seems like you just want to get the value of A which should not be equal to 0 . Read comments
double A=0;
do {
System.out.println("Set the A param: ");
if(input.hasNextDouble() == true) { //check for valid value
A = input.nextDouble();
if(A == 0) {
System.out.println("Param A cannot be a 0!");
}
} else{ // no valid value found , print msg and jump over the previous input
input.nextLine();
System.out.println("Param A must be a number!");
}
} while(A == 0); // just check , if the desired value is received
// previously input.hasNextDouble() had no use cuz we already
// checked no double value found so will be false, don't use it
First, you should not write the opposing check in an else if. Just use else. And you shouldn't check == true, since the value is already a boolean.
Now, for you infinite loop problem, when hasNextDouble() is false, it means that the user entered something wrong (ignoring the potential end-of-stream issue). In that case you need to discard that bad input, which is best done by calling nextLine().
Java naming convention states that variables should start with lowercase letter, so A should be a.
Your code then becomes:
double a = 0;
do {
System.out.println("Set the A param: ");
if (input.hasNextDouble()) {
a = input.nextDouble();
if (a == 0) {
System.out.println("Param A cannot be a 0!");
}
} else {
input.nextLine(); // discard bad input
System.out.println("Param A must be a number!");
}
} while (a == 0);
Thank you for both answers, after first I wrote this:
do
{
System.out.println("Podaj wartość parametru A: ");
if(input.hasNextDouble() == true)
{
A = input.nextDouble();
if(A == 0)
{
System.out.println("Parametr A nie może być zerem!");
}
}
else if(input.hasNextDouble() == false)
{
System.out.println("Parametr A musi być liczbą!");
input.nextLine();
A = 0;
}
} while(A == 0);
And it worked but thanks to the second answer now I know how to do it better. :)
Thanks both of you once again.
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.
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 ]
while( first !='y' || first !='n') is always true.
Replace your code with:
while( first !='y' && first !='n')
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')
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;
}
}
I tried using a loop to get a proper desired input from the user. The loop just doesn't stop. I brainstormed for a while but could not patch the bug.
char choice;
System.out.println("Below is a auto generated description for your property.Is it okay for you? (y/n)");
choice = sc.next().charAt(0);
for(;(choice!='y' || choice !='Y' || choice!='n' || choice !='N' );)
{
choice = sc.next().charAt(0);
System.out.println("Please enter 'y' or 'n'.");
}
// ... other codes ... //
Please help!
Thank you.
choice != 'y' || choice != 'Y'
The above test will always be true. If choice is y, then choice != 'Y' is true, and the whole condition is thus also true. If choice is Y, then choice != 'y' is true, and the whole condition is thus also true.
You want && instead of ||.
Also, for (; condition;) is more readable when written as while (condition).
for(; !(choice=='y' || choice =='Y' || choice=='n' || choice =='N' ) ;)
{
choice = sc.next().charAt(0);
System.out.println("Please enter 'y' or 'n'.");
}
Just add a !. In my opinion, while loop is more suitable here.
while(user did not enter y or n){
// loop
}
Use below code
you should use && operator inside for or while loop.
Scanner sc = new Scanner(System.in);
char choice;
System.out.println("Below is a auto generated description for your property.Is it okay for you? (y/n)");
choice = sc.next().charAt(0);
while(choice!='y' && choice !='Y' && choice!='n' && choice !='N' ){
System.out.println("Please enter 'y' or 'n'.");
choice = sc.next().charAt(0);
}
I am trying to get a valid input of "y", "Y", "n" , or "N".
If the input is not valid (for example any word that starts with a "y" or "n") I want it to re-prompt the user for input.
So far I have:
while (again.charAt(0) != 'N' && again.charAt(0) != 'n' && again.charAt(0) !='Y' && again.charAt(0) != 'y' ) {
System.out.println ("Invalid Inpur! Enter Y/N");
again = numscan.next();
}
if (again.charAt(0)== 'N' || again.charAt(0) == 'n') {
active = false;
} else {
if (again.charAt(0)== 'Y' || again.charAt(0) == 'y'){
active = true;
random = (int) (Math.random () *(11));
}
}
The problem I am having is if I enter any word that starts with the letter "y" or "n" it senses it as valid input (since it is the character at slot 0). I need help fixing this so I can re-prompt the user when they enter a word that starts with a "y" or "n".
Thanks!
Assuming again is a string containing the complete user input, you could use:
while (!again.equals("N") && !again.equals("n") ...
The .equals() method will match only if the entire string matches.
You could just test to make sure the length of the input is 1:
again.length() == 1
But a better approach might be:
while (! (again.equalsIgnoreCase("n") || again.equalsIgnoreCase("y"))) {
...
}
or even
while (! again.matches("[nyNY]")) {
...
}
One of the way would be:
First check again String length is only ONE character. If not, ask again.
if(again.length() ==1)
{
while (again.charAt(0) != 'N' && again.charAt(0) != 'n' && again.charAt(0) !='Y' && again.charAt(0) != 'y' ) {
System.out.println ("Invalid Inpur! Enter Y/N");
again = numscan.next();
}
.....
}else
{
System.out.println ("Invalid Inpur! Enter Y/N");
again = numscan.next();
}
It sounds like what you want is:
while (!again.equals("N") && !again.equals("n") && !again.equals("Y") && !again.equals("y") ) {
System.out.println ("Invalid Inpur! Enter Y/N");
again = numscan.next();
}
This way you can also easily add Yes/No, etc later if you want.
Regex could be an alternative to have strict input checks. Following piece of code validates y or n ignoring the case.
while (!again.matches("(?i)^[yn]$")){
System.out.println ("Invalid Inpur! Enter Y/N");
again = numscan.next();
}
active = (again.equalsIgnoreCase("Y"))? true : false;