1

I have a Phone class that has only one field: an ArrayList of instances of the Contact class. The Contact class has only two fields: name and number.
When you run the program,

  • It displays numbered instructions
  • You enter a number
  • It calls the function the performs the corresponding action using a switch statement, like so:

    switch (num) {
        case 0:
            printInstructions();
            break;
        case 1:
            addContact();
            break;
    }

The switch statement is in a while loop. I've put the code to prompt the user for information (like a name) inside the functions:


    public static void addContact() {
        System.out.print("Name: ");
        String name = scanner.nextLine();
        scanner.nextLine();
    
        System.out.print("Number: ");
        String number = scanner.nextLine();
        phone.addContact(name, number);
    }

phone.addContact():


    public void addContact(String name, String number) {
        //there is a check to make sure the contact doesn't already exist
        contacts.add(new Contact(name, number));
    }

My problem: I can't get it to save the name I enter. The value of name stays an empty string no matter what I enter. What's stranger is that the number is saved correctly. Why is this happening?


Edit: Also, It works fine if I get rid of the loop and the switch statement and the function and run the code directly in main().

(Is there any other code I should share?)

I was asked to include the Contact class, so —


    public class Contact {
        private String name;
        private String number;
    
        public Contact(String name, String number) {
            this.name = name;
            this.number = number;
            System.out.println("Contact created with name " + name + " and number " + number);
        }
    
        public Contact(String name) {
            this(name, null);
        }
    
        public Contact() {
            this("J. Doe", null);
            System.out.println("ERROR: blank contact somehow generated");
        }
    
        public String getName() {
            return name;
        }
    
        public String getNumber() {
            return number;
        }
    
        @Override
        public String toString() {
            return "Contact { \n" +
                    "    name = \"" + name + "\",\n" +
                    "    number = \"" + number + "\"" +
                    "\n}";
        }
    }

And the while loop:


    while (true) {
                System.out.print("Enter an instruction number: ");
                num = phoneScnr.nextInt();
                switch (num) {
                    case 0:
                        printInstructions();
                        break;
                    case 1:
                        addContact();
                        break;
                    case 2:
                        editContact();
                        break;
                    case 3:
                        removeContact();
                        break;
                    case 4:
                        findContact();
                        break;
                    case 5:
                        listContacts();
                        break;
                    case -1:
                        quit = true;
                        break;
                    default:
                        System.out.println("Sorry, that character is not recognized.");
                        break;
                }
            }

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83

1 Answers1

4

try not reading the scanner's Line again after reading the Name :

public static void addContact() {
    System.out.print("Name: ");
    String name = scanner.nextLine();
    //scanner.nextLine(); <-- here is the reason

    System.out.print("Number: ");
    String number = scanner.nextLine();
    phone.addContact(name, number);
}
ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83