Having difficulty ignoring case sensitivity for do while loop - java

Trying to let the user exit the program using the letter 'N' or 'n' but it will only let them exit when using 'N'
Below is my code, any help is appreciated!!
System.out.print(" Do you want to repeat the ");
System.out.println("program ['Y' or 'N']");
Choice = sc.next().charAt(0);
} while (Choice != 'N' || Choice != 'n');
}
}

You have to use while (Choice != 'N' && Choice != 'n'); .
It must be AND condition. Not an OR condition.

You are using the wrong operator:
while (Choice != 'N' && Choice != 'n');
Right now, your code states that the loop will continue when the character is either not 'N' or not 'n'. Since the character can't be 'N' and 'n' at the same time, the loop will always continue. Switching the operator to a && requires that the character is neither N nor n to continue.

Related

input character inside a while loop with validation

System.out.print("Enter the operator (+ - X /): ");
operator = input.next();
char c=operator.charAt(0);
while (c != '+' && c != '-' && c != '*' && c != '/'){
System.out.println("Operator doesn't match. Try again.");
System.out.print("Enter the operator (+ - X /): ");
input.next().charAt(0);
}
Here, I want an input character value from keyboard which will be only symbols just (+ - * /) inside a while loop. if the sign is not match the while loop will be running.
Here, the while loop is working but the character is not checked. So, while loop continuously works with-
System.out.println("Operator doesn't match. Try again.");
System.out.print("Enter the operator (+ - X /): ");
In the last line of your while loop, you are retrieving an user input, but not storing it anywhere. You should do like that:
c = input.next().charAt(0);
If you want to do something fancy, you could also try using the do-while loop, like below:
char c;
do {
System.out.println("Enter a operator (+ - * /):");
c = input.next().charAt(0);
} while(c != '+' && c != '-' && c != '*' && c != '/');

Initialising Issue

private void northroom()
{
char choice2;
boolean boolean_1 = false;
String answer2;
System.out.println("You press a panel next to the door");
System.out.println("The panel lights up with a pinging sound");
System.out.println("The door slowly scrapes open");
System.out.println("You are now in North Room");
System.out.println("There is a object in the coner of the room shoraded by darkness");
do
{
System.out.println("Would you like to pick up object Yes(Y) or No(N)");
while(boolean_1 == false)
{
answer2 = keyboard.next();
choice2 = answer2.charAt(0);
if(choice2 == 'Y')
{
System.out.println("You go pick up the object");
}
if(choice2 == 'N')
{
System.out.println("Stare at object because you are useless");
System.out.println("Try again");
}
}
}
while (**choice2** != 'Y' && **choice2** != 'N');
while (choice2 != 'Y' && choice2 != 'N');
Here both the choice2 are initialised how do I correct this so that it loops properly
The compilers code-path analyzer doesn't go far enough to recognize that the while(boolean_1 == false) loop will always execute at least once.
As far as the compiler is concerned, the loop body might be skipped, in which case the choice2 variable is unassigned, i.e. not definitely assigned.
You can fix that by initializing it to a dummy value, e.g.
char choice2 = ' ';
Of course, if the compiler did extended analysis, it would have found that the while(boolean_1 == false) loop runs forever, since boolean_1 is never updated, and would have generated compile error saying that while (choice2 != 'Y' && choice2 != 'N'); is unreachable.
I'm not sure what is the designed behavior for your nested loops. However, ur inner loop is a infinite loop. It can never end so outer loop can never check condition showed in last line of code

Java while loop not working with input checking of characters

The code below is from a game in which I need to generate 2 random numbers in java, and the user is presented with one of them. They then have to guess if the second number is higher or lower than the first. I created a while loop to check for errors in their input and the loop is executed even if the condition is not true. Entering H means their guess is the next number is higher, and L means they believe its lower
// Input
System.out.println ("Do you think the second number is higher or lower (enter 'H' or 'L' respectively): ");
char userGuess = Character.toUpperCase(input.next().charAt(0));
// Input error check (and immediate output of second number)
while (userGuess != 'H' || userGuess != 'L') {
System.out.println ("Sorry, I didn't understand that. Please try again: ");
userGuess = Character.toUpperCase(input.next().charAt(0));
}
System.out.println ("The second number was " + secondRandomNumber);
Use && not ||. Of course it's not going to be H or not going to be L
while (userGuess != 'H' && userGuess != 'L') {
System.out.println ("Sorry, I didn't understand that. Please try again: ");
userGuess = Character.toUpperCase(input.next().charAt(0));
}

Stopping a loop when receiving a specific character for input

I have written code that changes uppercase letters to lowercase letters and vice versa. I'm trying to use break so that I can exit the loop whenever the input is '.', and it doesn't seem to be working. Any advice would be appreciated! I also want to create a count of all the time the loops reiterated before it ended. How would I go about that?
public static void main(String[] args) throws java.io.IOException {
char choice, ignore = 0;
for (; ; ) {
do {
System.out.print("Please enter a upper or " + "lower case letter(. to quit)");
choice = (char) System.in.read();
if ((char) choice > 64) {
ignore = (char) (choice + 32);
}
if ((char) choice > 96) {
ignore = (char) (choice - 32);
}
System.out.print(ignore);
System.out.println("\n");
if (choice == '.') {
break;
}
do {
ignore = (char) System.in.read();
} while (ignore != '\n');
} while ((char) choice > 64 | (char) choice < 123 | choice != '.');
}
}
The problem is you have a nested loop. Your for (; ; ) will not be exited because the break will only escape from:
do {
//other code
} while ((char) choice > 64 | (char) choice < 123 | choice != '.');
You have a few options but probably the easiest would be to change the for loop to something like this:
while(choice != '.'){
//do other code
}
This way when the break is reached your code will exit the do while and exit the while on the next loop.
Keep in mind with this technique you will have to initialize choice with a value.
You have a do-while loop inside a permanent for-loop. The break statement is going to break you out of the do-while loop, but you're still in the for-loop. It will just re-enter the do-while loop again and keep going.
Even though your while loop breaks, the for loop keeps on running forever. One fix may be to add the terminating condition within for loop.
for (; choice != '.'; )
Please make sure to initialize the choice variable or else it would throw error during compile.
stackoverflow.com/q/886955/4793951
Break labels
Thanks! #Zircon

Comparing char in a while loop --- condtion is never met

So I have a really annoying problem which I am hoping one of you could help solve for me. This is a really simple program that prints my COMP science username in asterisks. I have attached a choice for the user - to either print the username in asterisks or simply print the characters.
I have constructed a while loop that validates the data entered by the user is accurate. The conditions for this while loop are never met - so it always loops through it no matter what is entered. I'm sure this is a really simple problem, just haven't really used chars before so can't figure out what I am doing wrong.
//===================================================== START OF MAIN
public static void main (String [] args){
Scanner input = new Scanner(System.in); // Scanner for users input
char usersChoice = 0; // Variable for users input
System.out.println("\nWould you like to see the large letters or the small letters?\n (Enter L for large, S for small!!)\n");
usersChoice = input.next().charAt(0); // Users Input
System.out.println(usersChoice);
//================================================= WHILE LOOP TO CHECK AUTHENTICITY OF DATA
while (usersChoice != 'l' || usersChoice != 'L' || usersChoice != 's' || usersChoice != 'S'){
System.out.println("\nWrong Input - Please try again!!!\n");
usersChoice = input.next().charAt(0);
System.out.println(usersChoice);
}//end of while
//================================================= IF (CHAR = L) PRINT BIG LETTERS
if (usersChoice == 'L' || usersChoice == 'l'){
printU();
print4();
printJ();
printA();
}//end of if
//================================================= ELSE PRINT LETTERS
else{
System.out.println("\nU");
System.out.println("4\n");
System.out.println("J\n");
System.out.println("A\n");
}//end of else
}//end of main
The while statement expression is always true since not all expressions can be true at once - you need the conditional && operator
while (usersChoice != 'l' && usersChoice != 'L' && usersChoice != 's' && usersChoice != 'S') {
Your logical or(s) should be logical and(s), this
while (usersChoice != 'l' || usersChoice != 'L' || usersChoice != 's' ||
usersChoice != 'S')
Should be
while (usersChoice != 'l' && usersChoice != 'L' && usersChoice != 's' &&
usersChoice != 'S')
The problem with your while loop is there is no character that could meet the conditions. Consider lower case l, when the usersChoice is l it's not L so it wouldn't finish.
while (!(usersChoice != 'l' || usersChoice != 'L' || usersChoice != 's' || usersChoice != 'S'))
Just add an exclamation mark before your while-loop.
Your while-loop always do not work because:
if userChoice is 'l', it is not 'L'/'s'/'S' (expression is true)
if userChoice is 'L', it is not 'l'/'s'/'S' (expression is true)
if userChoice is 's', it is not 'l'/'L'/'S' (expression is true)
if userChoice is 'S', it is not 'l'/'L'/'s' (expression is true)
Your while-loop always evaluates to true

Categories

Resources