0

I am currently learning Java programming using an Udemy course (Complete Java Masterclass by Tim Bulchalka to be exact) and am learning ArrayList.

I am trying to program an app to behave like an mobile phone, in which it can contain contact numbers.

Here is my main src(or what do you call it?), minus some irrelevant (or codes which encounter the same problem):

package com.timbuchalka;

import java.util.Scanner;

public class Main {

private static Scanner scanner2 = new Scanner(System.in);

public static void main(String[] args) {
    MobilePhone phone = new MobilePhone();
    boolean quit = false;
    int choice = 0;
    printInstructions();
    while(!quit) {
        System.out.println("What do you want to do?");
        choice = scanner2.nextInt();
        scanner2.nextLine();

        switch (choice) {
            case 1:
                phone.printContact();
                break;
            case 2:
                phone.addContact();
                break;
            ........
            case 7:
                quit = true;
                break;
        }
    }
}

public static void printInstructions() {
    System.out.println("\nPress ");
    System.out.println("\t 0 - To see instructions again.");
    System.out.println("\t 1 - To see all contacts.");
    System.out.println("\t 2 - To add new contact.");
    ......
    System.out.println("\t 7 - To quit the application.");
}

}

Here is my Contacts class (within MobilePhone class):

package com.timbuchalka;

import java.util.ArrayList;
import java.util.Scanner;

public class Contacts {

private static Scanner scanner = new Scanner(System.in);

private ArrayList<Integer> listPhoneNumber = new ArrayList<Integer>();
private ArrayList<String> listName = new ArrayList<String>();

public void addContact() {
    System.out.println("Please enter the contact name: ");
    String name = scanner.nextLine();
    System.out.println("Please enter the contact number: ");
    int number = scanner.nextInt();
    addContact(name, number);
}

public void printContacts() {
    System.out.println("Contact Details:");
    for (int i=0; i<listPhoneNumber.size(); i++) {
        System.out.println((i+1) + ". " + listName.get(i) + " - " + listPhoneNumber.get(i));
    }
    System.out.println("\t");
}

private void addContact(String name, int number) {
    listName.add(name);
    listPhoneNumber.add(number);
}

When going through the application, the 1st round works fine. I can print and/or add a contact to the my Contacts class. However, when I try to add another contact, I am unable to input the Contact Name. The console skips straight to inputting inputting the Contact Number instead. Here is an example:

Press 
     0 - To see instructions again.
     1 - To see all contacts.
     2 - To add new contact.
     3 - To modify a contact's name.
     4 - To modify a contact's number.
     5 - To remove a contact's details.
     6 - To search for a contact's details.
     7 - To quit the application.
What do you want to do?
1
Contact Details:

What do you want to do?
2
Please enter the contact name: //the 1st time it doesn't skip this step
John
Please enter the contact number: 
12345
What do you want to do?
2
Please enter the contact name: // however during the 2nd time, this step is skipped
Please enter the contact number: 
3
What do you want to do?
1
Contact Details:
1. John - 12345
2.  - 3

May I know why is this happening? And also, may I know how do I resolve this issue? Thank you very much!

2 Answers2

1

When reading from a Scanner I find it best to always use nextLine() and then manually parse the data.

This allows you to consistently consume whole lines of data. And you can more carefully parse the user input.

So, in your case, instead of reading the option using nextInt(), you'd use nextLine() and then Integer.parseInt().

jjnguy
  • 128,890
  • 51
  • 289
  • 321
0

Every time you call scanner.nextInt() follow it with scanner.nextLine() so that the end of line token is swallowed.