Sorry for this dumb question, but I have written a code with the use of logical question (nested if statements), the second statement basically asks for yes or no and either one of these has an operation to be executed, but if the user enters a different input other than that, it outputs "enter a valid input", the problem I am having right now is even if I entered either yes or no, it still reads the operation inside the else statement, instead of executing from the if/else if statements.
I have tried placing the close() method in below or above the if statements, have tried creating separate Scanner objects for 3 variables and also I tried converting some input from char to String but it still didn't worked out.
Here is the sample of the code
package drinkingJava;
import java.util.Scanner;
public class Entrance {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
Scanner y = new Scanner(System.in);
Scanner z = new Scanner(System.in);
String surname;
String answer;
char gender;
System.out.println("What is your surname?:");
surname = x.next();
System.out.println("Enter your gender (M/F):");
gender = y.next().charAt(0);
System.out.println("Do you have a $50?:");
answer = z.nextLine();
x.close();
y.close();
z.close();
if (gender == 'M' || gender == 'm') {
if (answer == "yes" || answer == "Yes" || answer == "y" || answer == "Y") {
System.out.println("Hello Mr. " + surname + "!, Please come in to our hotel!!");
} else if (answer == "no" || answer == "No" || answer == "n" || answer == "N") {
System.out.println("Sorry Mr. " + surname + ", we can't let you in for now.");
} else {
System.out.println("Please enter a valid answer.");
// it still outputs "enter a valid answer" even if I entered yes/no in the answer.
}
} else if (gender == 'F' || gender == 'f') {
if (answer == "yes" || answer == "Yes" || answer == "y" || answer == "Y") {
System.out.println("Hello Ms. " + surname + "!, Please come in to our hotel!!");
} else if (answer == "no" || answer == "No" || answer == "n" || answer == "N") {
System.out.println("Sorry Ms. " + surname + ", we can't let you in this time.");
} else {
System.out.println("That was an invalid answer");
}
} else {
System.out.println("You have entered an invalid gender.");
}
}
}