简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM