简体   繁体   中英

Program asks the same response twice?

My Student Records program asks the user to enter the student's first name once in the do while loop. The user has the option to enter the same data again by typing yes and the same question "Enter student's first name: " appears twice one on top of the other.

I inserted breakpoints in that while loop and once the user chooses "yes" at the end of the do while it goes up to the first line in the first while loop, then the second line , the third, and back up to the first again, then the second, the third and breaks into the next while loop asking for the students's last name.

Why is my program spitting that first question twice one on top of the other during the second time of the do while loop?

package assignment_3;

import java.util.Scanner;

/**
 * @author Diego
 */
public class Demo {

    public static void main (String args[]) {

    //Instantiating new recordsObject
        StudentRecords recordsObject = new StudentRecords();
        String firstName = "";
        String lastName = "";
        String idNumber = "";
        String major = "";         

        Scanner keyboard = new Scanner(System.in);   

        //Asking the user to enter the students' info

        do {

            while ( firstName.isEmpty() ) {
                System.out.println("Enter student's first name: ");
                firstName = keyboard.nextLine();   
            }

           while ( lastName.isEmpty() ) {
                System.out.println("Enter student's last name: ");   
                lastName = keyboard.nextLine();
            }

            while ( idNumber.length() != 9 ) {
                System.out.println("Enter student's nine-digit ID#: ");
                idNumber = keyboard.nextLine();
            }

            while ( major.isEmpty() ) {
                System.out.println("Enter student's major: ");
                major = keyboard.nextLine();
            }

            //Concatinating first name and last name into name
            String name = firstName + " " + lastName;

            //Adding the new entry to the StudentArrayList
            StudentEntry entry = new StudentEntry (name ,idNumber, major);
            recordsObject.addStudentEntry (entry);

            //Resetting variables
            firstName = "";
            lastName = "";
            idNumber = "";
            major = "";

           /*If the user enters "yes" then they can submit more.
            If they enter "no", then all submitted info will be shown */
            System.out.println("Type yes to add another entry");
        } while (keyboard.next().equalsIgnoreCase("YES"));
        keyboard.close();

        //Printing out those entries

        System.out.println("Student Records");
        System.out.println("---------------");
        for (int index = 0 ; index < recordsObject.StudentArrayList.size() ; index++) {
            System.out.println(recordsObject.StudentArrayList.get(index));
            System.out.println("");
        }
    }
}

The problem is caused by the use of keyboard.next() in your main while loop. Next does not read the line feed. Hence, when the user types "yes", the code to read the next firstName will automatically read the remaining newline character, still leaving firstName as empty. And, since firstName remains empty, it will prompt a second time.

Change:

} while (keyboard.next().equalsIgnoreCase("YES"));

To:

} while (keyboard.nextLine().equalsIgnoreCase("YES"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM