0

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();
    }
}
2
  • What kind of trouble do you have exactly ? Commented Nov 25, 2016 at 15:12
  • I am unable to utilize the do-while loop to fill out the array, and after the initial loop the I am no longer prompted to enter the scientists name. @Berger Commented Nov 25, 2016 at 15:14

4 Answers 4

1

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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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();

Comments

0

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;}
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.