2

I'm currently working on a java project. I'm making a phone-book. I use switch to select whether the user wants to input the number or a name. The problem is that when I use the switch, which tells the user to input the number it works just fine, but when I use the 'choice' which makes the user input the String it doesn't work. In the run box I can't input the String. Pls help. Here's the code. case 1 and case 3 aren't working.

int choice = scan.nextInt();
switch(choice){
    case 1:
        System.out.println("\nWho would you like to call?");
        name = scan.nextLine();

        CallContact(name);
        break;

    case 2: 
        System.out.println("\nWhich coontact You Want to Search?");
        break;

    case 3: 
        System.out.println("\nWhich Name You Want to Save?");
        name = scan.nextLine();
        System.out.println("\nWhat is the Number of the person you want to save?");
        long number = scan.nextLong();

        SaveContact(name, number);
        break;

    default:
    }
}
1

2 Answers 2

1

First of all you forgot put break in the default case but it does not effect to solve your usecase. This behavior is happen because when you are taking input with scan.nextInt() its set pointer at the end of that particular line. So just make habit if you are taking input of int then immediate you want to take input of string then just add extra scan.nextLine() before next input.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String name = new String();
    int choice = scan.nextInt();
    switch (choice) {

        case 1:
            scan.nextLine();// changes
            System.out.println("\nWho would you like to call?");
            name = scan.nextLine();

            CallContact(name);
            break;


        case 2:
            scan.nextLine();
            System.out.println("\nWhich Contact You Want to Search?");
            name = scan.nextLine();// changes

            break;

        case 3:
            scan.nextLine();// changes
            System.out.println("\nWhich Name You Want to Save?");
            name = scan.nextLine();
            System.out.println("\nWhat is the Number of the person you want to save?");
            long number = scan.nextLong();

            SaveContact(name, number);
            break;


        default:
            break;//improve
    }

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

3 Comments

it worked. but i didnt really get what was the problem, could you please clarify a bit more.
When you use scan.nextLine() it set scanner pointer at the end of that particular line.example :-10_. When your call scan.nextLine() after nextInt() it starts from _ and consider \n as a input of nextLine() so initially it get line input as 'empty line' and we thought it skipped nextLine().
feel free to mark it answer if answer solve your problem.
1

Always use scan.nextLine() to receive the input and then convert received input in your desired format.

int choice = Integer.parseInt(scan.nextLine());

1 Comment

You should spend a bit of time explaining how/why this solves what problem.

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.