When I run my program using the "!choice.equals("6")", it runs my main menu once tho that doesn't execute the function that is selected and nothing happens, which exits immediately. However, if my condition in the while loop is "choice.equals("6")", it executes one of my function which the main menu re-appears and exits when I select from 1-5 without me even entering 6 at all. The menu won't call other function that is selected. I'm kinda of bummed that both condition wouldn't work in this program I'm working on.
I used the debbuger, when I stepped into the while condition it wouldn't let me passed "!choice.equals("6")". I was expecting my main menu to keep asking me to enter a choice before I decided to exit the program.
private void mainMenu()
{
String input = JOptionPane.showInputDialog("Persona Library Main Menu\n" +
"1. Add from this list:\n" +
" -Language\n" +
" -Fiction\n" +
" -Geography\n" +
"2. Search Item By ID\n" +
"3. Remove this from Library\n" +
"4. Random Books\n" +
"5. Print complete list or by category\n" +
"6. Exit Program");
String choice = input;
while(!choice.equals("6"));
{
switch(choice)
{
case "1":
addToLibrary();
break;
case "2":
searchByIDtoPrint();
break;
case "3":
removeFromLibrary();
break;
case "4":
//undecided;
break;
case "5":
searchCategoryToPrint();
break;
case "6":
//undecided
break;
}
input = JOptionPane.showInputDialog("Persona Library Main Menu\n" +
"1. Add from this list:\n" +
" -Language\n" +
" -Fiction\n" +
" -Geography\n" +
"2. Search Item By ID\n" +
"3. Remove this from Library\n" +
"4. Random Books\n" +
"5. Print complete list or by category\n" +
"6. Exit Program");
choice = input;
}
}
From what I can read - the condition while(!choice.equals("6")) will only run when choice doesn't equal 6, which means that if you don't enter 6 at your first input selection, it will never enter the loop and simply go to the next input prompt.
Since the input prompts are not inside of loops after the second time you ask them to enter a number the program will reach the end of the function and I assume terminate - this is where some of your problems may lie, you haven't told it to do anything after the second prompt.
May I suggest a do-while loop as in:
do { /*Enter input prompts and case statements here*/ } while (program == true)
and only setting input if you select the option to close the program. This way, you gurantee the program to run at least once and if you never select exit - you simply just return to the prompt.
*EDIT - Also, have just noticed you never change choice in the while loop which means you'll be in an infinite loop.
I hope this answers your question.