0

I having an issue with my code skipping input fields in a loop when I select to enter in more data.

Below is my loop:

do {
    System.out.println("****PLEASE ENTER DETAILS FOR PERSON "+count+"****");
    count++;
    System.out.print("Name: ");
    name = keyboardIn.nextLine();
    System.out.print("Gender: ");
    gender = keyboardIn.next().charAt(0);
    System.out.print("Age: ");
    age = keyboardIn.nextInt();
    keyboardIn.nextLine();
    System.out.print("Occupation: ");
    occupation = keyboardIn.nextLine();
    System.out.print("Weight: ");
    weight = keyboardIn.nextDouble();
    System.out.print("Height: ");
    height = keyboardIn.nextDouble();
    //Add the person object to the ArrayList
    personList.add(new Person(ppsNo++, name, age, gender, occupation, height, weight));       
    System.out.print("Would you like to add another person record (Y/N): ");
    ans = keyboardIn.next().charAt(0);
} while (ans == 'y' || ans == 'Y');  //continue entering people if user enters y or Y 

This is the output when I try to enter information for another person:

****PLEASE ENTER DETAILS FOR PERSON 1****
Name: Nevin O'Regan
Gender: M
Age: 43
Occupation: IT
Weight: 89
Height: 78
Would you like to add another person record (Y/N): Y
****PLEASE ENTER DETAILS FOR PERSON 2****
Name: Gender: 
Michail Alexakis
  • 1,405
  • 15
  • 14
NevSter79
  • 1
  • 1
  • At the bottom of the loop, `ans = keyboardIn.next()` will leave a `\n` inside the Scanner's buffer, so you need to add a `keyboardIn.nextLine()` below it to eat the leftover `\n`. To understand it more in depth, see: https://stackoverflow.com/a/13102066/7254424 – thorin9000 May 20 '22 at 21:30
  • I've tried that but now if I select 'Y' at the end to enter a second person it doesn't go back through the loop at all. – NevSter79 May 20 '22 at 21:43
  • Does the line below `ans = keyboardIn.next().charAt(0);` look like `keyboardIn.nextLine();` – thorin9000 May 20 '22 at 21:48
  • yes but I'm still facing that issue. I've had to remove keyboardIn.nextLine(); after the name variable and replace it with next(); Not ideal but it will do for now – NevSter79 May 20 '22 at 23:36
  • @NevSter79 -- my I ask why it is not ideal? – Kemper Lee May 21 '22 at 01:00
  • @KemperLee it doesn't allow me to enter in fullName only firstName. So if I try to input name = John O'Brien it skips, but if I enter John it doesn't skip. – NevSter79 May 24 '22 at 11:06

1 Answers1

0

Here's an example program of what I meant by my comment - it takes input for person 2's name.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner keyboardIn = new Scanner(System.in);
        int count = 1;
        String name = "", occupation = "";
        char gender = '-', ans;
        double weight = 0, height = 0;
        int age = 0, ppsNo = 1;

        List<Person> personList = new ArrayList<>();
        do
        {
            System.out.println("****PLEASE ENTER DETAILS FOR PERSON "+count+"****");
            count++;
            System.out.print("Name: ");
            name = keyboardIn.nextLine();
            System.out.print("Gender: ");
            gender = keyboardIn.next().charAt(0);
            System.out.print("Age: ");
            age = keyboardIn.nextInt();
            keyboardIn.nextLine();
            System.out.print("Occupation: ");
            occupation = keyboardIn.nextLine();
            System.out.print("Weight: ");
            weight = keyboardIn.nextDouble();
            System.out.print("Height: ");
            height = keyboardIn.nextDouble();

            //Add the person object to the ArrayList
            personList.add(new Person(ppsNo++, name, age, gender, occupation, height, weight));

            System.out.print("Would you like to add another person record (Y/N): ");
            ans = keyboardIn.next().charAt(0);
            keyboardIn.nextLine();
        }while(ans == 'y' || ans == 'Y');
        keyboardIn.close();
    }

    private static class Person {
        public Person(int ppsNo, String name, int age, char gender, String occupation, double height, double weight) {
            
        }
    }
}

PLEASE ENTER DETAILS FOR PERSON 1 Name: Nevin O'Regan Gender: M Age: 43 Occupation: IT Weight: 89 Height: 78 Would you like to add another person record (Y/N): Y PLEASE ENTER DETAILS FOR PERSON 2 Name: Jay Gender: M Age:

thorin9000
  • 171
  • 1
  • 6