Reputation: 13
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
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();
}
}
Upvotes: 0
Views: 1130
Reputation: 191
always prefer to read input using nextLine() and then parse the string.
Using next()
will only return what comes before a space. nextLine()
automatically moves the scanner down after returning the current line.
A useful tool for parsing data from nextLine()
would be str.split("\\s+")
.
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.nextLine().equalsIgnoreCase("Y"));
input.close();
}
}
Upvotes: 1
Reputation: 296
Another way to do it, which i find simpler is with an arraylist, and a regular while loop that breaks after you change a boolean. See following example:
ArrayList<String> scientists = new ArrayList<String>();
Scanner input = new Scanner(System.in);
boolean keepGoing = true;
while(keepGoing){
System.out.println("Enter the name of the Scientist: ");
scientists.add(input.nextLine());
System.out.println("Would You like to add another Scientist? (y/n)");
if(input.nextLine().toLowerCase().equals("y")){continue;}
else{keepGoing = false;}
}
Upvotes: 0
Reputation: 70
Is this the solution that you mean
String scientists[] = new String[100];
int scientistCount = 0;
Scanner input = new Scanner(System.in);
boolean again = true;
while(again){
System.out.println("Enter the name of the Scientist: ");
scientists[scientistCount]=input.nextLine();
scientistCount++;
System.out.println(scientistCount);
System.out.println("Would You like to add another Scientist? y/n");
if(!input.nextLine().equalsIgnoreCase("y")){
again = false;
}
}
input.close();
Upvotes: 0
Reputation: 422
Change while(input.next().equalsIgnoreCase("Y"));
to while(input.nextLine().equalsIgnoreCase("Y"));
Upvotes: 0