I have to make a loop taking a users input until “done” is entered

前端 未结 4 1211
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 01:53

I\'m trying to make an ArrayList that takes in multiple names that the user enters, until the word done is inserted but I\'m not really sure how.

相关标签:
4条回答
  • 2020-12-20 02:12
    ArrayList<String> names = new ArrayList<String>();
    String userInput;
    Scanner scanner = new Scanner(System.in);
    while (true) {
        userInput = scanner.next();
        if (userInput.equals("done")) {
            break;
        } else {
            names.add(userInput);
        }
    }       
    scanner.close();
    
    0 讨论(0)
  • 2020-12-20 02:16

    I would probably do it like this -

    public static void main(String[] args) {
      System.out.println("Please enter names seperated by newline, or done to stop");
      Scanner scanner = new Scanner(System.in);     // Use a Scanner.
      List<String> al = new ArrayList<String>();    // The list of names (String(s)).
      String word;                                  // The current line.
      while (scanner.hasNextLine()) {               // make sure there is a line.
        word = scanner.nextLine();                  // get the line.
        if (word != null) {                         // make sure it isn't null.
          word = word.trim();                       // trim it.
          if (word.equalsIgnoreCase("done")) {      // check for done.
            break;                                  // End on "done".
          }
          al.add(word);                             // Add the line to the list.
        } else {
          break;                                    // End on null.
        }
      }
      System.out.println("The list contains - ");   // Print the list.
      for (String str : al) {                       // line
        System.out.println(str);                    // by line.
      }
    }
    
    0 讨论(0)
  • 2020-12-20 02:21
    String[] inputArray = new String[0];
    do{
      String input=getinput();//replace with custom input code
      newInputArray=new String[inputArray.length+1];
      for(int i=0; i<inputArray.length; i++){
        newInputArray[i]=inputArray[i];
      }
      newInputArray[inputArray.length]=input
      intputArray=newInputArray;
    }while(!input.equals("done"));
    

    untested code, take it with a grain of salt.

    0 讨论(0)
  • 2020-12-20 02:37
        ArrayList<String> list = new ArrayList<String>();
        String input = null;
        while (!"done".equals(input)) {
            //  prompt the user to enter an input
            System.out.print("Enter input: ");
    
            //  open up standard input
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    
            //  read the input from the command-line; need to use try/catch with the
            //  readLine() method
            try {
                input = br.readLine();
            } catch (IOException ioe) {
                System.out.println("IO error trying to read input!");
                System.exit(1);
            }
            if (!"done".equals(input) && !"".equals(input))
                list.add(input);
        }
        System.out.println("list = " + list);
    
    0 讨论(0)
提交回复
热议问题