如何在 Java 中用新行拆分每个输入?

How to split each input by a new line in Java?

我正在尝试从用户那里获取 5 个字符串输入,这些输入将存储在一个数组中。当我输入 "Hello World" 之类的内容并换行时,我只能再输入 3 个单词。所以我希望每个用户输入都是一个句子,然后按回车键应该要求用户在新行上输入另一个内容。

到目前为止,这是我的代码:

Scanner user_input = new Scanner(System.in);   
String ask1 = user_input.next()+"\n";
String ask2 = user_input.next()+"\n";
String ask3 = user_input.next()+"\n";
String ask4 = user_input.next()+"\n";
String ask5 = user_input.next();
String[] cars = {ask1, ask2, ask3, ask4, ask5};    

我建议您有一个特定的关键字或短语,用户可以键入这些关键字或短语来停止程序。在这里,我做了一个简单的程序,它使用 java.util.Scanner 对象来接收键盘输入。每个值都存储在一个名为 "inputs." 的 java.util.ArrayList 中。当用户完成输入时,he/she 将键入 "stop",程序将停止。

import java.util.*; //you need this for ArrayList and Scanner

public class Input{

    public static void main(String[] args){

        Scanner user_input = new Scanner(System.in); //create a scanner object
        ArrayList<String> inputs = new ArrayList<String>(); //I used a java.util.ArrayList simply because it is more flexible than an array
        String temp = ""; //create a temporary string which will represent  the current input string

                while(!((temp = user_input.next()).equals("stop"))){ //set temp equal to the  new input each iteration
                inputs.add(temp); //add the temp string to the arraylist
                }
    }
}

如果要将 ArrayList 转换为普通 String[],请使用此代码:

String[] inputArray = new String[inputs.size];
for(int i = 0; i < inputs.size(); i++){
    inputArray[i] = inputs.get(i);
} 

您可以通过将您的问题存储在一个数组中并循环遍历提示输入的 for 循环,直到您有问题为止,从而使它更通用。这就是为什么当您有更多问题时可以将它们添加到列表中而无需更改代码上的任何其他内容。

然后,要回答有关创建字符串数组的原始问题,您可以使用以下方法 String[] a = answers.toArray(new String[answers.size()]);

import java.util.ArrayList;
import java.util.Scanner;

public class HelloWorld
{
  public static void main(String[] args)
  {
    ArrayList<String> questions = new ArrayList<String>(5){{
        add("What is your name?");
        add("What is school you went to?");
        add("Do you like dogs?");
        add("What is pats name?");
        add("Are you batman?");
    }};

    ArrayList<String> answers = new ArrayList<String>(questions.size()); // initialize answers with the same size as question array
    String input = ""; // Stores user input here
    Scanner scanner = new Scanner(System.in);
    for(String question : questions){
      System.out.println(question); // Here we adding a new line and the user type his answer on a new line
      input = scanner.nextLine();
      answers.add(input); // Store the answer on answers array
    }
    System.out.println("Thank you.");
    String[] a = answers.toArray(new String[answers.size()]); // THis converts ArrayList to String[]
    System.out.println("You entered: " + a.toString());
  }
}

你想要这个:

Scanner user_input = new Scanner(System.in);   
String ask1 = user_input.nextLine()+"\n";
String ask2 = user_input.nextLine()+"\n";
String ask3 = user_input.nextLine()+"\n";
String ask4 = user_input.nextLine()+"\n";
String ask5 = user_input.nextLine();
String[] cars = {ask1, ask2, ask3, ask4, ask5};  

根据documentationScanner.next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

由于 Scanner 使用的默认分隔符是空格,因此调用 next() 将从用户输入中获取单个单词。当你想捕获多个以换行符结尾的单词时,你应该使用 Scanner.nextLine() 代替。

此外,您可以通过预先创建数组并在循环中分配用户输入条目来消除代码重复(您始终应该这样做,保持内容 DRY):

final int numberOfCars = 5;
Scanner userInput = new Scanner(System.in);   
String[] cars = new String[numberOfCars];

for (int i = 0; i < numberOfCars; i++) {
    cars[i] = userInput.nextLine();
}