I am trying to build a program that accepts only \"Yes\" or \"No\" as correct in
ID: 641708 • Letter: I
Question
I am trying to build a program that accepts only "Yes" or "No" as correct input. If I typed "Yes" or "No" as my initial input, the program does what I intend to do; however, If I typed an incorrect value like -2 and then I typed either "Yes" or "No" the program does not capture these inputs and consider them as an incorrect input.
Scanner input = new Scanner(System.in);
int yesNoCount = 1;
System.out.println("Do you want to continue (y/n)");
String answer = input.next();
while (yesNoCount <= 3) {
if (answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("Yes")) {
System.out.println("You pressed Yes. Program continues");
break;
}
if (answer.equalsIgnoreCase("N") || answer.equalsIgnoreCase("No")) {
System.out.println("Thank you for using the program. Bye Bye!");
System.exit(0);
}
if ((!answer.equalsIgnoreCase("Y")) || (!answer.equalsIgnoreCase("Yes")) || (!answer.equalsIgnoreCase("N")) || (!answer.equalsIgnoreCase("No"))) {
++yesNoCount;
System.out.println("Wrong input. Try again!!");
input.next();
}
if (yesNoCount == 3) {
System.out.println("You made more than three mistakes. Good Bye!");
System.exit(0);
}
} // while loop ends here
System.out.println("If user type Yes, the program continues reading this line of code");
Please kindly comment on the error and provide me feedback to enhance the above code. Thank you in advance for your assistance!
Explanation / Answer
Suggession to improve the code:
1. In this code, else if condition should be used, so that if any condition is satisfied then it will not check other conditions in while loop and will go for another iteration as per input.
2. The given input shoud be validated first then it should check other condition to improvr the perfprmance the code.
3. After that No condition should be checked so that if user give input as no then code will be immedatly terminated without cheking any more condtion below that.
Please find the code below with commented error and suggestion:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int yesNoCount = 0;
/* Commenting both the below line as it should be under while loop.
* So if user gives wrong input then yesNoCount will increase from the first input given.
*/
/*System.out.println("Do you want to continue (y/n)");
String answer; = input.next();
*/
String answer;
while (yesNoCount <= 3) {
/*adding the code here which was commented above..*/
System.out.println("Do you want to continue (y/n)");
answer = input.next();
/* Here we should use && instead of || as we need to check that if all condition is not satisfied then only code will go inside the block..*/
if ((!answer.equalsIgnoreCase("Y")) && (!answer.equalsIgnoreCase("Yes")) && (!answer.equalsIgnoreCase("N")) && (!answer.equalsIgnoreCase("No"))) {
yesNoCount++;
System.out.println("Wrong input. Try again!!");
//input.next();
}
else if (answer.equalsIgnoreCase("N") || answer.equalsIgnoreCase("No")) {
System.out.println("Thank you for using the program. Bye Bye!");
System.exit(0);
}
else if (answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("Yes")) {
System.out.println("You pressed Yes. Program continues");
System.out.println("If user type Yes, the program continues reading this line of code");
/* Commented break.. as it will break to loop and come out from the loop.*/
// break;
}
if (yesNoCount >= 4) {
System.out.println("You made more than three mistakes. Good Bye!");
System.exit(0);
}
/*This println should be under while loop so that is user give yes then this line will be prnted */
} // while loop ends here
}
}
// If you have question then please comment, I will help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.