I know this is something very simple, however, I've only been programming for a couple months so my brain just fog's out sometimes, and I need help with a while loop with nested else if statements. The problem my loop is continuous because the user is never given the chance to enter in their choice (which -1 stops the loop). How can I change the loop so that it operates by asking user to enter a choice (1-4, or -1 to quit)
Please Any help is appreciated. I know it's something simple, I've searched thorugh previous forum discussions but I can't seem to make it work.
//create new scanner object
Scanner userInput = new Scanner (System.in);
int end = 0;
//find out what user wants to do with address book
while (end != -1)
{
System.out.println(" ");
System.out.println(" ");
System.out.println("----------------------------");
System.out.println("What would you like to do with your address book? ...");
System.out.println("----------------------------");
System.out.println("Add new [Enter 1]");
System.out.println("Delete existing [Enter 2]");
System.out.println("Edit existing [Enter 3]");
System.out.println("Search for [Enter 4]");
System.out.println("EXIT [Enter -1]");
}
if (userInput.hasNext() && (userInput.nextInt() == 1))
{
AddressBookProcessor.addContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 2)
{
AddressBookProcessor.deleteContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 3)
{
AddressBookProcessor.editContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 4)
{
AddressBookProcessor.findContact();
}
else if (userInput.nextInt() != 1 || userInput.nextInt() != 2
|| userInput.nextInt() != 3 || userInput.nextInt() != -1)
{
System.out.println("Please enter a valid input");
end = -1;
}
}