0

I am trying to create a small/simple menu driven bank application for a school assignment. I am running a do-while loop to ask which command the user would like to use. After I get the users input and the loop runs once, the second time I display the list of commands it prints them twice.

do {
        System.out.println("Here is a list of possible commands:");
        System.out.println("Create Customer:'c'");
        System.out.println("Exit Application:'x'");

        read = keyboard.nextLine();

        if (read.equalsIgnoreCase("c")) {
            System.out.println("You have chosen to create a new customer!");

            System.out.println("\nPlease enter the customers first name: ");
            firstName = keyboard.nextLine();

            System.out.println("\nPlease enter the customers last name:");
            lastName = keyboard.nextLine();

            System.out.println("\nPlease enter the customers address: ");
            address = keyboard.nextLine();

            System.out.println("\nPlease enter the customers email: ");
            email = keyboard.nextLine();

            System.out.println("\nPlease enter the customers phone number: ");
            phoneNum = keyboard.nextLine();

            System.out.println("\nPlease enter the customers age: ");
            age = keyboard.nextInt();

            System.out.println("\nPlease enter the customers initial deposit: ");
            deposit = keyboard.nextDouble();

            Customer tempCust = new Customer(firstName, lastName, address, email, age, phoneNum);
            tempCust.createAccount(deposit);
            customers.add(tempCust);

            System.out.println("\nThank you! A new customer has been created!\n");
        }
    } while (!read.equalsIgnoreCase("x"));

The first time the loop runs it displays:

Here is a list of possible commands: Create Customer:'c' Exit Application:'x'

But the second time it runs it displays:

Here is a list of possible commands: Create Customer:'c' Exit Application:'x' Here is a list of possible commands: Create Customer:'c' Exit Application:'x'

Thanks a lot for any help!

Xpec
  • 9
  • 1

1 Answers1

0

Add read = keyboard.nextLine(); after yours : System.out.println("\nThank you! A new customer has been created!\n");

read = keyboard.nextLine();
System.out.println("\nThank you! A new customer has been created!\n");

You are pointing to something that is not equal to 'x' or 'c', you have to move the pointer to an empty line, otherwise your program will not wait for your input.

Rodrigo Ney
  • 353
  • 4
  • 20