0

I am working on code in which I am making an application that stores contacts by asking the user particular choice about operations he or she wants to perform like querying, storing, adding a contact, etc.

Below is sub-part of my code, I am actually trying to bug the issue when the user enters a wrong choice. What I mean to say is, if a user enters a non-numeric character, it should ask for the choice again,

package com.vakhariaKathan;
import java.util.Scanner;

public class Main
{
    private static Scanner scanner = new Scanner(System.in);
    private static MobilePhone mobilePhone = new MobilePhone();
    public static void main(String[] args)
    {
        int choice;
        boolean quit = false;
        System.out.println("Hey there! :),\nI am your Mobile Phone,");
        printInstructions();
        while(!quit)
        {
            System.out.print("Enter choice: ");
            if(!scanner.hasNextInt())
            {
                System.out.println("You entered something bad ");
                scanner.nextLine();
                continue;
            }
            choice = scanner.nextInt();scanner.nextLine();
            switch (choice)
            {
                case 0:
                    printInstructions();
                    break;
                case 1:
                    mobilePhone.printContacts();
                    break;
                case 2:
                    handleAdditionOfContact();
                    break;
                case 3:
                    System.out.print("Enter contact name to be changed: ");
                    mobilePhone.modifyContact(scanner.nextLine());
                    break;
                case 4:
                    System.out.println("Enter any name of the following you want to delete.");
                    mobilePhone.showContactNames();
                    mobilePhone.removeContact(scanner.nextLine());
                    break;
                case 5:
                    quit = true;
                    break;
                default:
                    System.out.println("Enter a valid choice!");
                    break;
            }

        }

    }

    private static void printInstructions()
    {
        System.out.println("Currently i can do following operations.");
        System.out.println("0.Print instructions.");
        System.out.println("1.Show list of contacts.");
        System.out.println("2.Add Contact.");
        System.out.println("3.Modify a contact.");
        System.out.println("4.Delete Contact.");
        System.out.println("5.Quit");
    }

    //to handle addition of contact
    private static void handleAdditionOfContact()
    {
        String[] output = takeInput();
        mobilePhone.addContact(output[0],output[1]);
    }

    //Method to take user input
    private static String[] takeInput()
    {
        String name,contact;
        System.out.print("Enter name of the Contact: ");
        name = handleEmptyEntry("Name");
        if(mobilePhone.doesExist(name))
        {
            System.out.println("That contact already exists.\nTry a different name");
            name = handleEmptyEntry("Name");
        }
        System.out.print("Enter contact number for " + name + ": ");
        contact = handleEmptyEntry("contact");
        return new String[] {name,contact};
    }
    //function to handle empty entry
    private static String handleEmptyEntry(String fieldName)
    {
        String in = scanner.nextLine();
        while (in.isBlank())
        {
            System.out.printf("%s field cannot be empty ‼\nEnter again:",fieldName);
            in = scanner.nextLine();

        }
        return in;
    }

}

There are mainly two scenarios:

1)It works fine if a user enters a wrong character without hitting the enter key. 2)But when the user enters a new line and then enters a wrong character, it shows some weird behaviors. Below are the outputs showing both cases.

Hey there! :),
I am your Mobile Phone,
Currently i can do following operations.
0.Print instructions.
1.Show list of contacts.
2.Add Contact.
3.Modify a contact.
4.Delete Contact.
5.Quit
Enter choice: p
You entered something bad 
Enter choice: 

i
You entered something bad 
Enter choice: You entered something bad 
Enter choice: You entered something bad 
Enter choice: 

Will be pleased to have a detailed explanation!

  • 2
    Does this answer your question? [How to use java.util.Scanner to correctly read user input from System.in and act on it?](https://stackoverflow.com/questions/26446599/how-to-use-java-util-scanner-to-correctly-read-user-input-from-system-in-and-act) – Goion Dec 09 '19 at 02:16

1 Answers1

0

If you have written scanner.next() then you wouldn't have faced this issue but when you write scanner.nextLine() you faced this behaviour because before entering your non numberic value you pressed two times enter and then entered the non numeric value. So you got You entered something bad 3 times. Two times for the enter you pressed and one time for non-numeric value. This happens because newline character remains in the buffer and when you enter non-numeric value it reads all of them and print You entered something bad.

So if you press enter 5 times and then enter non-numeric value you'll get You entered something bad 6 times printed.

Himanshu Singh
  • 2,005
  • 1
  • 3
  • 14