Dom R Dom R - 13 days ago 11
Java Question

Do-While loop to fill out Array

I am currently working on a project that asked me to set up data entry. In the data entry mode the user will be asked to enter the Scientists data in a loop. if the user responds with 'y' they will be prompted to enter another scientist. The best way I can think to do this is with a do-while loop to fill the array until the user decides its over. I am having trouble with


  1. filling the names into the array, and

  2. the program won't prompt for a name after the initial loop.



Here is what I have:

public class Scientist {
private String name;
private String field;
private String greatIdeas;

public static void main(String[] args) {
String scientists[] = new String[100];
int scientistCount = 0;
Scanner input = new Scanner(System.in);

do{
String answer;
System.out.println("Enter the name of the Scientist: ");
scientists[scientistCount]=input.nextLine();

System.out.println(scientistCount);
System.out.println("Would You like to add another Scientist?");
scientistCount++;
}

while(input.next().equalsIgnoreCase("Y"));
input.close();
}
}

Answer

Change while(input.next().equalsIgnoreCase("Y")); to while(input.nextLine().equalsIgnoreCase("Y"));