0

Whenever I run the program, everything works fine but for some reason the Confirmation prints are happening twice and I can't figure out why. Any help would be appreciated.

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String Z = "Z";
        int number;
        String Znumber = "";
        String Confirmation = "";
        int ExamType;

        // Ask user for Z number
        do {
            System.out.println("Please enter your Z number. Example: 12345678");
            number = input.nextInt();
            Znumber = Z + number;

        } while ((Znumber.length() != 9));

        //Confirm Z number entry
        do {
            System.out.printf(
                    "Your Z number is: %s. Is this correct? Enter Y/N: \n",
                    Znumber);
            Confirmation = input.nextLine();
        } while (!Confirmation.equalsIgnoreCase("y"));

        // Ask user which exam they would like to take
        ExamType = 0;
        Confirmation = "";

        System.out.println("Which exam would you like to take? Select 1-3: ");
        System.out.println("1: English");
        System.out.println("2: Spanish");
        System.out.println("3: Math");
        ExamType = input.nextInt();

        do {
            System.out.printf("You selected %s. Are you sure? Enter Y/N: \n",
                    ExamType);
            Confirmation = input.nextLine();

        } while (!Confirmation.equalsIgnoreCase("y"));

        // Begin exam
        if (ExamType == 1) {

            System.out.println("Welcome to the English exam!");
            // Start code from JavaExam.java

        } else if (ExamType == 2) {

            System.out.println("Welcome to the Spanish exam!");
            // Start code from MathExam.java

        } else if (ExamType == 3) {

            System.out.println("Welcome to the Math exam!");
            // Start code from EnglishExam.java
AlexR
  • 109,181
  • 14
  • 116
  • 194
  • Which message do yo call "confirmation message"? "Your Z number is..." or "You selected %s. Are you sure?" – AlexR Nov 25 '15 at 15:36
  • 1
    After you use `nextInt()` you need to call `nextLine()` to use up the rest of that input line. Otherwise `nextLine()` will immediately return its buffered input and your `do` loop will go to a second iteration. – khelwood Nov 25 '15 at 15:38

3 Answers3

0

Your first loop (// Ask user for Z number)

number = input.nextInt();

does not read the last '\n'.

I think you need to add a nextLine() in that loop.

cadrian
  • 7,095
  • 2
  • 30
  • 41
0

Instead of Confirmation = input.nextLine();, use Confirmation = input.next(); and you should be good. Tested and confirmed.

You really don't need nextLine here in your do-while loop.

hagrawal
  • 12,025
  • 4
  • 33
  • 61
0

In your do-while when you run for the first time print Your Z number is: %s. Is this correct? Enter Y/N:. after that the condition !Confirmation.equalsIgnoreCase("y") is evaluated in this case gives true for this cause try to run the loop do-while in second time.

     do {
       System.out.printf("Your Z number is: %s. Is this correct? Enter Y/N: \n",
               ....
        } while (!Confirmation.equalsIgnoreCase("y"));
Abdelhak
  • 8,161
  • 4
  • 19
  • 34