Validating a inputted char variable. Do-while loop will not break - java

I have a method that checks if the user is a student, but I can't get it validate the conditions.
char custStud = '0';
Scanner input = new Scanner(System.in);
do{
System.out.println("Are you a student? (Type Y or N): ");
custStud = input.next().charAt(0);
custStud = Character.toLowerCase(custStud);
}
while (custStud != 'y' || custStud != 'n');
When I fire up this program, it does not break the loop, even if 'y' or 'n' are entered. I suspect custStud might have accidentally changed types when changed to lowercase, but I'm not sure.
How can I make this loop work properly?

while (custStud != 'y' || custStud != 'n') is always true, since custStud can't be equal to both 'y' and 'n'.
You should change the condition to:
while (custStud != 'y' && custStud != 'n')

You've mistaken here:
while (custStud != 'y' || custStud != 'n');// wrong
while (custStud != 'y' && custStud != 'n');// correct
Try running this code:
char custStud = '0';
Scanner input = new Scanner(System.in);
do{
System.out.println("Are you a student? (Type Y or N): ");
custStud = input.next().charAt(0);
custStud = Character.toLowerCase(custStud);
}
while (custStud != 'y' && custStud != 'n');
System.out.print("\n answer:"+custStud);

Related

How to ask a question repeatedly until I get the right answer as a character

I should ask "choose" and the user should type a b or c if he types somehting else, it should repeat the question. I don't even understand how I do that and then I have another problem: Later I should print out the chosen sort of coffee but because the "eingabe" is in a while or if body it can't be recognized when I want to use it later. How do I solve this problem? Or should I use another loop anyway? And is character even the right type to use here?
Thx in advance.
while(eingabe != 'a' || eingabe != 'b' || eingabe ||'c') {
Out.println("Bitte auswaehlen: ");
char eingabe = in.readChar();
}
Out.print("Gewaehlt ");
if (eingabe == 'a') Out.print("CAPPUCCINO");
if (eingabe == 'b') Out.print("MOKKA");
if (eingabe == 'c') Out.print("VERLAENGERTER");
Out.print(" !");
}
As mentioned before you should declare it outside the loop. And you mistyped in the loop when check c:
char eingabe = '';
while(eingabe != 'a' && eingabe != 'b' && eingabe != 'c') {
Out.println("Bitte auswaehlen: ");
eingabe = in.readChar();
}
Out.print("Gewaehlt ");
if (eingabe == 'a') Out.print("CAPPUCCINO");
if (eingabe == 'b') Out.print("MOKKA");
if (eingabe == 'c') Out.print("VERLAENGERTER");
Out.print(" !");
Your eingabe variable should be defined outside of the loop, so the rest of your code can access it. Just be careful not to initialise it with a value checked for in the condition, or even better use a do-while instead.
I switched your code to use regular System.in and System.out, as I do not know what those in and Out are. Last but not least, your output condition was wrong, you should ask again if the value is is different from all expected values, not if differend from any (which is always true):
char eingabe;
do {
System.out.println("Bitte auswaehlen: ");
eingabe = (char) System.in.read();
} while (eingabe != 'a' && eingabe != 'b' && eingabe != 'c');
System.out.print("Gewaehlt ");
if (eingabe == 'a')
System.out.print("CAPPUCCINO");
if (eingabe == 'b')
System.out.print("MOKKA");
if (eingabe == 'c')
System.out.print("VERLAENGERTER");
System.out.print(" !");
I think there is a problem in your expression eingabe != 'a' || eingabe != 'b' || eingabe ||'c'. Maybe you meant eingabe != 'a' || eingabe != 'b' || eingabe != 'c'. I think in order to continue loop when user types something else, you need to use && instead of ||. A do while loop is ideal for your use case:
char eingabe;
Scanner in = new Scanner(System.in);
do {
System.out.println("Bitte auswaehlen: ");
eingabe = in.next().toCharArray()[0];
} while (eingabe != 'a' && eingabe != 'b' && eingabe != 'c');

Java: it keeps saying "variable yn might not have been initialized" [duplicate]

This question already has answers here:
Variable might not have been initialized error
(12 answers)
Closed 3 years ago.
I already initialized the variable yn, but it keeps saying that I didn't. Tried to initialized it directly on the scanner but it has error and say again that it's already been initialized in the method.
import java.util.Scanner;
public class sample
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num;
char yn;
while (yn == 'Y' && yn == 'y')
{
do
{
System.out.print("Enter a positive integer: ");
num = s.nextInt();
if (num < 0)
{
System.out.print(num + "is a negative integer. Please try again!");
}
else
{
if (num % 2 == 0)
{
System.out.println(num + " is an even number.");
}
else
{
System.out.println(num + " is an odd number.");
}
continue;
}
}
while (num < 0);
System.out.println("Press Y if you want to input again and N if no.");
yn = s.next().charAt(0);
do
{
if (yn == 'N' && yn == 'n')
{
System.out.println("Done!");
break;
}
else if (yn == 'Y' && yn == 'y')
{
System.out.println("Done!");
continue;
}
else
{
System.out.println("Invalid Input! Try again!");
break;
}
}
while (yn != 'Y' && yn != 'y' && yn != 'N' && yn != 'n');
}
}
}
You declare yn without an initial value and then immediately use it as loop condition.
char yn;
while(yn == 'Y' && yn == 'y'){
would imply you expect a default value of y (or Y). You must explicitly set it so for that to be true. Also, no character is both 'Y' and 'y' so you need a logical or. Like,
char yn = 'Y';
while(yn == 'Y' || yn == 'y') {
You might also consider
char yn = 'Y';
while (Character.toUpperCase(yn) == 'Y') {
and then you don't need an ||. You have the same logical impossibility here
if(yn == 'N' && yn == 'n'){
System.out.println("Done!");
break;
}
else if(yn == 'Y' && yn == 'y'){
System.out.println("Done!");
continue;
}
and can fix it with || (or with Character.toUpperCase(char)).
char yn;
This is where you declare the variable.
while(yn == 'Y' && yn == 'y'){
This is where you use the variable. Nowhere in between these two lines is yn initialized - that's why it's used uninitialized.
Think of "Yn" as a container, you need to have something in the container if you want to test what is inside that container, or in this case
char yn;
Should become
char yn = 'Y';
You test with your while loop and your if statements
char yn; //You are DECLARING a variable
char yn = 'Y' //You are Initialising a variable
In your case, you can say
char yn = ' ';
Because I believe that is what you tried to do
I also noticed you are trying to test if your yn variable is a capital and lowercase at the same time, you can fix this by changing the && (AND) to || (OR)
In Java whenever you declare a variable but do not initialize it (like in your case char yn;), the compiler checks for its initialization part further.
If you later initialize it inside any scope which has a condition (like in your case while (yn == 'Y' && yn == 'y'){..}), then compiler is not able to decide at compile time that whether the code inside the scope or block will get executed or not.
Hence there is no assurance to compiler although you have initialized inside the block.
Therefore compiler throws an error stating, variable not initialized.

Why is ((ans != 'N') || (ans != 'Y')) always true?

I'm trying to prompt the user to spin again and my "while" expression is always coming back as false... Anyone have any ideas?
reSpin = false;
if (reSpin == false){
System.out.println("Would you like to spin again? Y/N");
char ans = in.next().charAt(0);
if (ans == 'Y'){
reSpin =true;
}else if (ans == 'N'){
System.out.println("Thank you for playing!");
}else {
while ((ans != 'N') || (ans != 'Y')) {
System.out.println("Invalid answer, please only enter Y/N");
System.out.println("Would you like to spin again? Y/N");
ans = in.next().charAt(0);
}
}
}
You probably want to use:
while ((ans != 'N') && (ans != 'Y')) {
That checks to see that ans is not N and not Y. If you use || (or) there, then it will check to see that ans is either not N, or not Y (which is true for any value of ans).
Pretty simple: According to your comparison, ans would have to be Y and N at the same time.
|| returns true if either one of the expressions on either side is true.
If ans is Y, then ans != 'N' is true, so the whole expression (ans != 'N') || (ans != 'Y') is true. If ans is N, then ans != 'Y' is true, so the whole expression is true.
You want (ans != 'N') && (ans != 'Y'), which says "ans is not 'N' and ans is also not 'Y'."
ans cannot be both 'N' and 'Y' at the same time, so it is always either not equal to 'N' or not equal to 'Y'. You might want to change it to:
while ((ans != 'N') && (ans != 'Y'))
This makes sure that it is both not equal to 'N' and not equal to 'Y'.

loop does not work. could not find the symantic error

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

Multiple conditions in WHILE loop

I want to exit the while loop when the user enters 'N' or 'n'. But it does not work. It works well with one condition but not two.
import java.util.Scanner;
class Realtor {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
char myChar = 'i';
while(myChar != 'n' || myChar != 'N'){
System.out.println("Do you want see houses today?");
String input = sc.next();
myChar = input.charAt(0);
System.out.println("You entered "+myChar);
}
}
}
You need to change || to && so that both conditions must be true to enter the loop.
while(myChar != 'n' && myChar != 'N')
Your condition is wrong. myChar != 'n' || myChar != 'N' will always be true.
Use myChar != 'n' && myChar != 'N' instead
If your code, if the user enters 'X' (for instance), when you reach the while condition evaluation it will determine that 'X' is differente from 'n' (nChar != 'n') which will make your loop condition true and execute the code inside of your loop. The second condition is not even evaluated.

Categories