0

The below code is meant to populate an arraylist of employee objects with user input (first name, last name, job title, and sex). During the second loop the code asks for the first name input again, like expected, but doesn't allow the user to input it, prompting the user to input for last name instead. What am I doing wrong?

for(int i = 0; i < 2; i++){ //LOOP MESSES UP SOMEHOW
    //Get employee object first Name
    System.out.println("First name: ");
    fName = kb.nextLine();

    //Get employee object last Name
    System.out.println("\nLast name: ");
    lName = kb.nextLine();

    //Get employee object Job Title
    System.out.println("\nJob Title: ");
    job = kb.nextLine();

    //Set employee object sex
    while(loopControl){

        System.out.println("\nSex: (Enter M for Male or F for Female)");
        empSex = kb.next().charAt(0);

        //Validate sex value input
        if(empSex == 'M' || empSex == 'F'){
            loopControl = false;
        }
        else{
            System.out.println("Invalid entry. Enter M for Male or F for Female \n");
        }
    }

    //Add populated employee object to ArrayList
    employees.add(new EmployeeRegistry(fName, lName, job, empSex));
}
Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90
  • The last think you call in the 'loopControl' loop is next, that means there is a newLine waiting. So on the second iteration, when you ask for the first name, it just grabs the new line and goes on to the next question. – matt Jul 06 '20 at 09:33

0 Answers0