This loop keeps giving me my error message even when I enter a valid input and I can't find what is wrong with it. It's the same as my other loops in the program which all work fine. Does anyone know the problem? Here is the loop:
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
while((gender != 'm') || (gender != 'f')) {
System.out.println("ERROR Please enter a valid age");
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
}//end while
Now if I type 'm' or 'f' it will give me the error message? ??? ???
while((gender != 'm') || (gender != 'f')) {
should really be
while((gender != 'm') && (gender != 'f')) {
The condition (gender != 'm') || (gender != 'f') always evaluates to true. To understand why, consider one case - gender = 'm'...
(gender != 'm') || (gender != 'f')
('m' != 'm') || ('m' != 'f')
false || true
true
Basically, java doesn't know the common English idiom if gender isn't (either) male or female.
Related
This is quite a beginner question but I'm wondering why my do...while loop is not closing.
The program is supposed to loop while the user input is not 'C', 'c', 'F', or 'f'.
It seems to close when just one boolean expression in the while section is valid but not if multiple are valid.
public class CelsToFaren
{
public static void main(String[] args)
{
// scanner setup
Scanner sc = new Scanner(System.in);
// Variable declarations
int celsius;
int answerC;
int farenheit;
int answerF;
char userLetter;
do
{
// initial menu options
System.out.println("Which temperature would you like to convert from? ");
System.out.println(" >(C)elsius ");
System.out.println(" >(F)arenheit ");
// user input of C, c, F, or f to select option
userLetter = sc.next().charAt(0);
// if user input C or c
if ((userLetter == 'C' || userLetter == 'c'))
{
System.out.print("Please enter the temperature: ");
celsius = sc.nextInt();
answerC = ((celsius*9/5)+32);
System.out.println("The answer is: " + answerC + " Farenheit ");
}
else
{
// if user input F or f
if ((userLetter == 'F' || userLetter == 'f'))
{
System.out.print("Please enter the temperature: ");
farenheit = sc.nextInt();
answerF = ((farenheit-32)*5/9);
System.out.println("The answer is: " + answerF + " Celsius ");
}
else
{
// if user input not F, f, C, or c
if ((userLetter != 'F' || userLetter != 'f' || userLetter != 'C' || userLetter != 'c'));
{
System.out.println("Please enter a valid option");
}
}
}
} while ((userLetter != 'c') || (userLetter != 'C') || (userLetter != 'f') || (userLetter != 'F'));
}
}
You need to change the exit logic.
In your case 1 | 0 | 0 = true so the loop continues.
You need to change it to:
while ((userLetter != 'c') && (userLetter != 'C') && (userLetter != 'f') && (userLetter != 'F'));
Your condition is wrong. Lets assume you want to break loop in if statement. It would look like
if(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F')
Now let's apply negation to get a condition under which you do not need to exit the loop
if(!(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F'))
this condition is simillar to
if(userLetter != 'c' && userLetter != 'C' && userLetter != 'f' && userLetter != 'F')
Why is the first if statement always true?
private String setDepartment (){
int code = Integer.parseInt(JOptionPane.showInputDialog("Enter The Department Code:\n" +
"1:Sales\n" +
"2:Development\n" +
"3:Accounting\n" +
"4:None"));
/*Why this if statement is always true? How do i solve it? */
if (code !=1 || code !=2 || code !=3 || code !=4)
{
JOptionPane.showMessageDialog(null, "Invalid Number.Enter a number between 1-4");
setDepartment();
}
if (code==1){
return "Sales";
}
else if (code==2){
return "Development";
}
else if (code==3){
return "Accounting";
}
else
return "";
}
Replace || with &&:
if (code !=1 && code !=2 && code !=3 && code !=4)
You need to use AND instead of OR.
If the user enters 1 then it's automatically different than 2,3 and 4.
You can therefore use :
if (code !=1 && code !=2 && code !=3 && code !=4)
OR
if (code ==1 || code ==2 || code ==3 || code ==4)
From first principles, picking code = 1:
code !=1 || code !=2 || code !=3 || code !=4
= 1 !=1 || 1 !=2 || 1 !=3 || 1 !=4
= false || true || true || true
= true
You probably meant && rather than ||.
Because at any point of time code value would be any of 1-4. And 3 of 4 conditions would always be true. Change your if condition to
If( !( code ==1 || code == 2 || code == 3 || code == 4))
My goal is to get specific inputs from the user (A, B, C, and D only).
for example: If i enter the letter A, the if statement will execute (its not supposed to). and the same with the do while.
(Logic Error)
char[] response = new char[20];
Scanner k = new Scanner(System.in);
//Prompts User to fill array with their responses
for(int i=0; i<response.length; i++) {
//Input validation
do {
System.out.println("Answer "+(i+1)+":");
response[i] = k.nextLine().charAt(0);
if(response[i] != 'A' ||response[i] != 'B' || response[i] != 'C' ||response[i] != 'D')
System.out.print("Try Again. ");
}
while(response[i]!= 'A' ||response[i] != 'B' ||response[i] != 'C' ||response[i] != 'D');
}
This is how I would write it
Scanner in = new Scanner(System.in);
char[] response = new char[20];
//Prompts User to fill array with their responses
for(int i = 0; i < response.length; i++) {
for (;;) {
System.out.println("Answer " + (i + 1) + ":");
response[i] = in.nextLine().charAt(0);
//Input validation
if ("ABCD".indexOf(response[i]) >= 0)
break;
System.out.print("Please try again, it must be A, B, C or D");
}
}
What you were doing wrong is you needed to write
if (!(response[i] == 'A' || response[i] == 'B' || response[i] == 'C' || response[i] != 'D'))
OR
if (response[i] != 'A' && response[i] != 'B' && response[i] != 'C' && response[i] != 'D')
You condition is backwards. When you get it A you will get
if (false || true || true || true)
which will pass.
Change this to:
if(response[i] != 'A' && response[i] != 'B' && response[i] != 'C' && response[i] != 'D')
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
I am trying to write a program that forms a random 2 character word and then asks the user to guess the word. After the user inputs a guess, the program should check the guess with the answer. The program should then give the user a hint depending on their guess in the form nAkB where nA is the right letters in the right position and kB is the right letters but in the wrong position. My problem is that my program won't show the hint when their are right letters but in the wrong position (say the answer is aD and the user guesses Dc) the output should be "Hint: 0A1B". Here is my code for the getHint Method:
public static String getHint(String guess, String answer){
String hint = "No";
if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "You win!";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) != answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(1) == answer.indexOf(guess.charAt(1))){
hint = "Hint: 0A2B";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.indexOf(guess.charAt(1)) &&
guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(1) == answer.charAt(1) && guess.charAt(0) == answer.indexOf(guess.charAt(0)) &&
guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0) &&
guess.charAt(1) != answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 0A1B";
}
else if(guess.charAt(1) == answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1) &&
guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 0A1B";
}
else{
hint = "Hint: 0A0B";
}
return hint;
It never outputs anything with a 1B or 2B. It only does 1A0B or 0A0B.
Your issue is code like guess.charAt(0) == answer.indexOf(guess.charAt(0)) . indexOf will return the index of the character. So you are comparing the index of a character (rhs) to the ASCII value of the character (lhs) and your if statements using this indexOf construction are not being entered.
Rather, consider something like this:
else if(answer.indexOf(guess.charAt(0))!=-1 && answer.indexOf(guess.charAt(1)) != -1{
hint = "Hint: 0A2B";
}
From Java API, indexOf returns -1 if the given character is not found within the string.