0

I'm probably missing something very simple but I'm new and it's just easier to ask for help. This is homework so you can give clues if you want but please understand I'm horrible at java.

Below is some code for an address book. I want the user to enter 1 to view the entries 2 to put another entry in the book or 3 to quit. I got the program to work but it didn't loop to ask the user what to do next. I then coded a switch statement and when the user selects 1 the program doesn't run the code associated with case 1: and it's the same with case 2. The program does validate my entry (I wrote a validator in a seperate class)

Did I miss something when coding it?

again I'm new so don't beat me up.

    import java.util.Scanner;

    public class AddressBookEntryApp 
    {

public static void main(String[] args) 
{

    //create new scanner
    Scanner ip = new Scanner(System.in);
    //welcome user to the address book application
    System.out.println("Welcome to the Address Book Application");
    System.out.println();
    int choice = 0;
    boolean quit = false;
    do
    {

        //have the user enter a menu number
        System.out.println("1 - List entires");
        System.out.println("2 - Add entry");
        System.out.println("3 - Exit");


        System.out.println();
        int menuNumber = Validator.getInt(ip, "Enter menu number: ", 1, 3);
        System.out.println();
        switch (choice)
        {
        case 1:

                AddressBookIO GetEntryObject = new AddressBookIO();
                GetEntryObject.getEntriesString();
                System.out.println(AddressBookIO.getEntriesString());
                break;

        case 2:


                String name = Validator.getEntry(ip, "Enter name: ");
                String email = Validator.getEntry(ip, "Enter email address");
                String phone = Validator.getEntry(ip, "Enter phone number: ");
                AddressBookEntry newEntry = new AddressBookEntry(name, email, phone);
                AddressBookIO.saveEntry(newEntry);
                break;


        }
}while (!quit);

    }
    }

6 Answers 6

3

choice is set to 0. Did you mean to switch on menuNumber?

Sign up to request clarification or add additional context in comments.

4 Comments

menuNumber is from the original code before I had the switch statement. can I remove that? I tried changing it to choice but it gave me a red line. Also it said I had to intialize choice. IF I set it to 1 will it stay as 1? What can I do about that?
Produce the exact code you typed that was in red when you changed menuNumber to choice.
@Raveline Sorry it took me so long to respond I'm getting ready for work. Anyway eclipse tells me it's a duplicate variable when I change menuNumber to choice
@Raveline Nevermind. I see why. It is because I declared choice as int at the beginning of my code and then again in the line where I am sending it to my validator.
0

You don't change the value of variable choice. Therefore it remains '0' as you initialized at the beginning. So your code never gets into the case statements. That's the problem . Try the following:

choice = Validator.getInt(ip, "Enter menu number: ", 1, 3);

Comments

0

You assign choice with 0 (int choice = 0;) and never changes it, so in the switch it doesn't match neither 1 nor 2.

It should probably be

switch (menuNumber)

2 Comments

That seems to work! So I didn't have to declare a choice at all then? I have been doing that for other programs so some of this is out of new habbit.
Or you can assign selected menu number directly to the varible 'choice'. That would work too. Check my answer for that.
0

Yes, you assigned the output of the Validator to menuNumber, but you are doing the switch on choice.

Comments

0

Shouldn't this line

int menuNumber = Validator.getInt(ip, "Enter menu number: ", 1, 3);

be converted to the following:

choice = Validator.getInt(ip, "Enter menu number: ", 1, 3);

Comments

0

All right, as stated before, the "choice" variable is not the input of the user ("menuNumber", I guess). That's a first issue.

Now the question is : why did you make this mistake ? Because your code is too complex. You'd write much better code if you read the advices from Martin Fowler's "Clean Code", or others. Two simple tips :

  • keep small functions, and it'll be much easier to identify bugs.
  • prefer a "while" than a "do / while", it's much easier to read.

Bust most importantly, learn to use the debugger, with it, you'd have found the bug in no time.

1 Comment

Thank you for the book suggestion I will check that out. I'm learning from Murach's Java SE6 and my code reflects the code in the book. I have been told my coding isn't very pretty on here before but I'll definately check out that book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.