Home > Net >  How can I get my output to only display a certain message when the users input is not 1 of 2 certain
How can I get my output to only display a certain message when the users input is not 1 of 2 certain

Time:04-13

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.

CodePudding user response:

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

CodePudding user response:

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");
}
  • Related