'How can I get my output to only display a certain message when the users input is not 1 of 2 certain strings (options)?

my code

I'm pretty much brand new to java and coding been practicing for a couple weeks now and I'm trying to figure out how to get my output to only display the else statement when NONE of the 2 'if' options have been inputted. for ex. i have 2 categories of food places, Mexican and American. When the user is prompted to enter a category it displays the array of restaurants just fine but I cant get the else statement to not pop up with the first if option. No matter what if the first 'if' option is inputted the else statement gets printed, how do i stop that and only have it print when 'American' or 'Mexican' isn't inputted.



Solution 1:[1]

This "else" referrers to second "if". Use "switch-case" construction instead with "else" value as default.

Solution 2:[2]

You should use an if-elseif-else block instead of if ... if-else. That's the issue.

if (/* code for mexican */) {
    System.out.println("mexican options...");
} else if (/* code for american */) {
    System.out.println("american options...");
} else {
    System.out.println("Sorry, no options available");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy
Solution 2 ilanfriedman