'Loop is skipping input fields
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:
Solution 1:[1]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | thorin9000 |
