do{
System.out.println("Would you like to enter another number? (y/n)");
restart = console.next().charAt(0);
if (!(restart=='y')||!(restart=='n')||!(restart=='N')||!(restart=='Y'))
{
System.out.println("You entered something other than the letters (y) or (n).");
}
else if (restart=='n'||restart=='N')
{System.out.println("Goodbye.");}
}
while (!(restart=='y')||!(restart=='n')||!(restart=='N')||!(restart=='Y'));}
while (restart=='y'||restart=='Y');}}
I want the user to enter upper or lower case y or n and if its y then restart the program which worked fine before i started to add something to catch anything that wasnt a y or n. I want upper or lower case n to display goodbye and nothing happen and anything other than y or n to ask again to enter y or n until it gets the corect input and instead it always displays the you entered something other than... message and repeatedly asks if ud like to enter another number
Another alternative that you may prefer is to write
while("YyNn".indexOf(restart) == -1)
which means "loop again if restart is not found in "YyNn" ".
Replace || with && when you use it with ! - otherwise you have a condition that's always true. restart is ALWAYS either not Y or not N, and sometimes both.
This
if (!(restart=='y')||!(restart=='n')||!(restart=='N')||!(restart=='Y'))
is the same as
if ((restart!='y')||(restart!='n')||(restart!='N')||(restart!='Y'))
So you probably want
if(!((restart=='y')||(restart=='n')||(restart=='N')||(restart=='Y')))
and this is the same as
if ((restart!='y')&&(restart!='n')&&(restart!='N')&&(restart!='Y'))
You can also make this if statement better by converting the value to upper or lower case letters and compare that. Then you would only have to write code for 'N' and 'Y' or 'n' and 'y'.
You should use && instead of || in first IF and While condition check.
do {
System.out.println("Would you like to enter another number? (y/n)");
restart = console.next().charAt(0);
if ((restart != 'y') && (restart != 'n') && (restart != 'N') && (restart != 'Y'))
{
System.out.println("You entered something other than the letters (y) or (n).");
}
else if (restart == 'n' || restart == 'N')
{
System.out.println("Goodbye.");
}
}
while ((restart != 'y') && (restart != 'n') && (restart != 'N') && (restart != 'Y'));
Well, first of all you do not need to create an option for capital and lowercase letters. Use .equalsIgnoreCase(). Use && instead of or because you are saying if they put yes or no it will restart.
Related
I'm very new to programming in general, and I'm having a bit of trouble with a program I'm writing in Java to help calculate my final grade in a class. This part of the program asks me what letter grade I would like to receive, and then determines if that input is valid. For example, if I typed into the keyboard that I wanted to receive a letter grade of "Z", because that is not a valid grade, I would like my program to output "Invalid Input" and exit. The code I have written below is not producing any syntax errors, but it outputs "Invalid Input" for every letter grade I choose, even A, B, and C (inputs that should be valid). Any help in understanding what's wrong is more than welcome.
Scanner input = new Scanner(System.in);
char desiredGrade
System.out.print("What letter grade do you want to achieve for the course? ");
desiredGrade = input.next().charAt(0);
if (desiredGrade != 'A' || desiredGrade != 'B' || desiredGrade != 'C'){
System.out.println("Invalid Input");
System.exit(0);
}
In addition to this, it would be helpful to not have to worry about case sensitivity with the inputs. I know I can use .ignoreCase() or .equalsIgnoreCase() with strings, but I'm not quite sure how to implement that with char.
see this Answer
For Upper- and Lowercase you can wrap your char in Character and then call toLowerCase and check the input and the expected value on Lowercase.
In your example
desiredGrade != 'A' || desiredGrade != 'B' || desiredGrade != 'C'
If you want to use the || operator you have to do it like that
if(!(desiredGrade == 'A' || desiredGrade == 'B' || desiredGrade == 'C')){
}
That way you check if the Input is A, B, or C and if not then exit
Lets say desiredGrade = 'A'
if (desiredGrade != 'A' || desiredGrade != 'B' || desiredGrade != 'C'){
The first condition will be false, but the second and third will be true. So
if (false || true || true)
Will result always in true.
The way to do it is using operator AND &&
if (desiredGrade != 'A' && desiredGrade != 'B' && desiredGrade != 'C'){
This way, if the user decides to input 'A' the operation will be
if (false && true && true){
Resulting in false. And if the user inputs 'Z', the operation will be
if (true && true && true){
That will result true and execute the Invalid input output.
EDIT
As it has been mentioned. The user may input 'a' (lowercase) for which condition desiredGrade = 'A' will be false since 'a' != 'A' (is not equal).
So it will be wise to convert desiredGrade to uppercase before the if statement.
Try out
(desiredGrade != 'A' && desiredGrade != 'B' && desiredGrade != 'C')
Basically, you want to check if desiredGrade is different than A and different than B and different than C, print out invalid input.
Try this:
if (! Arrays.asList ('A', 'B', 'C').contains (Character.toUpperCase (desiredGrade))) {
// your error handling
}
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')
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;
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.