在java中读取多个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27878276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:12:52  来源:igfitidea点击:

Reading multiple strings in java

javajava.util.scanner

提问by user4424606

I have to read strings from the user based on a number nand store nstrings in ndifferent variables. I'm stuck with how to put them into different strings. Please help me out.

我必须根据一个数字从用户那里读取字符串n并将n字符串存储在n不同的变量中。我被困在如何将它们放入不同的字符串中。请帮帮我。

This is my code:

这是我的代码:

public static void main(String[] args) {
      int b;
      String s="";

      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();

      for (int i=0; i<=b; i++) {

          System.out.println("Enter a string: ");
          s = in.nextLine();
      }

So if b = 5,i have to input 5 strings from the user and store them in 5 different string variables. I'm able to take it from the user but not able to assign them into different variables. Can u please help me out? Thanks.

因此,如果b = 5,我必须从用户输入 5 个字符串并将它们存储在 5 个不同的字符串变量中。我可以从用户那里获取它,但不能将它们分配到不同的变量中。你能帮我一下吗?谢谢。

回答by SMA

Create an array and store it in an array like below:

创建一个数组并将其存储在一个数组中,如下所示:

String s[] = new String[b];//use b+1 if you need b+1 entries
for (int i=0; i<b; i++) {//use <=b is you need b+1 entries
      System.out.println("Enter a string: ");
      s[i] = in.nextLine();
}

You can then access your values as:

然后,您可以通过以下方式访问您的值:

for (int i=0; i<b; i++) //use <=b is you need b+1 entries
      System.out.println("Entered string was : " + s[i]);
}

回答by ha9u63ar

From your code (<= b) I am assuming you just started learning Java. Therefore, I edited your solution and am proposing the following, if this is okay?

从您的代码 (<= b) 我假设您刚刚开始学习 Java。因此,我编辑了您的解决方案并提出以下建议,是否可以?

public static void main(String[] args) {
      int b;


      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();
      //necessary to do due to Enter key pressed by user
      in.nextLine();
      String s[] = new String[b];
      for (int i=0; i<b; i++) {

          System.out.println("Enter a string: ");
          s[i] = in.nextLine();

          // You can check at the same time if this is what you entered
          System.out.println("I have received this sring:  "+s[i]+"\n");
      }

回答by Thusitha Thilina Dayaratne

If you know exactly the number of input then use an Array, if not use a ArrayList

如果您确切知道输入的数量,则使用 Array,如果不知道则使用 ArrayList

With Arrays

使用数组

  String []inpupts = new String[b];
  for (int i=0; i< b; i++) {
      System.out.println("Enter a string: ");
      inputs[i] = in.nextLine();
  }

With ArrayList

使用 ArrayList

  List<String> inpupts = new ArrayList<String>();
  for (int i=0; i< b; i++) {
      System.out.println("Enter a string: ");
      inputs.add(in.nextLine());
  }

回答by Sam

Solution :

解决方案 :

You can use something like this.

你可以使用这样的东西。

You can change your code as :

您可以将代码更改为:

public static void main(String[] args) {
      int b;
      String s="";

      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();
      in.nextLine(); // To get a new line

      for (int i=0; i<b; i++) {

          System.out.println("Enter a string: ");
          s = in.nextLine();
      }