0

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;
    }




}
user2044988
  • 33
  • 1
  • 1
  • 7

3 Answers3

3

Move your if/else's inside the while loop.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • thanks, my brain is getting tired and everything has to be in tonight, just been seeing code non-stop for 4 days straight! – user2044988 Mar 19 '13 at 00:43
1

You don't change the end variable inside the while loop, so that results in an infinite loop. You will need to place your input processing logic inside the while loop so that end has a chance to change, so that the while loop can end.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Simply putting the if statement inside the loop should work (note I haven't checked anything else on the code this is just a quick response):

//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;
    }
    }

}
0x6C38
  • 6,796
  • 4
  • 35
  • 47