【问题标题】:unable to take all the inputs into string array in java无法将所有输入放入java中的字符串数组
【发布时间】:2020-09-09 05:02:49
【问题描述】:

我已经编写了一个代码来获取字符串输入并存储在 java 中的字符串数组中。我的问题是,当我将字符串数组的大小声明为 3 时,它必须将三个字符串放入字符串数组,但它只将两个字符串放入字符串数组并且不接受第三个字符串。谁能帮我解决这个问题?

代码

import java.util.Scanner;
public class leet14 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n;
        System.out.println("enter length of string");
        n=sc.nextInt();
        String a[]=new String[n];
        System.out.println("enter string values");
        for(int i=0;i<a.length;i++){
            a[i]=sc.nextLine();
        }
        for(int j=0;j<a.length;j++){
           System.out.println(a[j]); 
        }
        }
}

输出

enter length of string
3
enter string values
one
two

one
two

【问题讨论】:

  • @Tzvi2 我已经试过了。它显示数组越界异常
  • @Tzvi2 for循环是正确的; for(int i=0; i &lt; a.length; i++) 循环 3 次,从 0 到 2,正确。
  • 我怀疑你需要一个 sc.nextLine() 在 for 循环之前使用你之前使用扫描仪读取整数的换行符。

标签: java arrays string java.util.scanner


【解决方案1】:

您必须在开始从控制台读取输入字符串之前添加sc.nextLine()

import java.util.Scanner;
public class leet14 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n;
        System.out.println("enter length of string");
        n=sc.nextInt();
        String a[]=new String[n];
        sc.nextLine();
        System.out.println("enter string values");
        for(int i=0;i<a.length;i++){
            a[i]=sc.nextLine();
        }
        for(int j=0;j<a.length;j++){
           System.out.println(a[j]); 
        }
    }
}

这会给你想要的结果。

输入:

enter length of string
3
enter string values
one
two
three

输出:

one
two
three

【讨论】:

    【解决方案2】:

    当您阅读int 并且想要阅读完整的String 时,您必须将Scanner 内的指针移动到下一行。 P.S.如果你调试你的程序很容易找到。

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    
        System.out.print("enter length of string: ");
        String[] arr = new String[scan.nextInt()];
        scan.nextLine();    // <-- move to the next line to be ready to read a whole string
    
        System.out.println("enter string values:");
    
        for (int i = 0; i < arr.length; i++) {
            System.out.format("line %d: ", i + 1);
            arr[i] = scan.nextLine();
        }
    
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);
    }
    

    输出:

    enter length of string: 3
    enter string values:
    line 1: one one
    line 2: two two
    line 3: three three
    one one
    two two
    three three
    

    【讨论】:

      【解决方案3】:

      我建议看一下这段代码,另外,尽量不要使用诸如n之类的值名称,它确实可以提高可读性并帮助您看到问题。

      import java.util.Scanner;
      
      public class Main {
      public static void main(String[] args) {
          
          Scanner sc=new Scanner(System.in);
          int inputCount=0;
          System.out.println("enter length of string");
          try{
              inputCount=sc.nextInt();
          }catch(Exception  e){
              System.out.println("int please");
              System.exit(1);
          }
          String a[]=new String[inputCount];
          System.out.println("enter string values");
          
          for(int i=0;i<a.length;i++){
              a[i]=sc.next();
          }
          for(int j=0;j<a.length;j++){
             System.out.println(a[j]); 
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        要获得所需的流程,只需将 a[i]=sc.nextLine(); 更改为 a[i]=sc.next(); 。除非您的字符串输入中有空格,否则应该可以正常工作,否则将 sc.nextLine(); 放在 for 循环之前

        这就是我看到问题的方式;

        nextInt(); 期望输入的第一个单词并将其存储为int,否则会引发错误,但不会跳出当前行。

        所以当你运行 n=sc.nextInt(); 并给出输入时

        enter length of string
        3
        

        3 被保存到n 但系统光标仍然像这样留在它后面,3|

        N.B:输出流和输入流不同,所以System.out.println("enter string values");运行没关系,输入流的光标仍然停留在3后面;

        enter length of string
        3|
        enter string values
        

        a[i]=sc.nextLine() 期望并存储光标后的句子,否则它会中断到下一行。但如果光标已经在新行上,它就有机会输入一个新句子(然后根据需要存储它)并移至新行。

        所以由于光标3|后面没有句子,所以在i = 0时,在for循环中将断行(换行)命令"\n"赋值给a[0]

        所以,由于光标现在在新行上,它提供了输入新句子的选项并将其存储到a[1],当点击输入i = 1并移动到下一行时,相同的过程是当i = 2时执行

        那么String[] a的内容显示如下;

        enter length of string
        3|   //after, nextInt() when i=0, since the cursor is not the first, it breaks the line, a[0] = "n" 
        enter string values
        |one //when  i=1, since it's the firstit gives chance for input, and stores to a[1]
        |two  //when  i=1, since it's the firstit gives chance for input, and stores to a[1]
             //a[0] is displayed
        one  //a[1] is displayed
        two  //a[2] is displayed
        

        我很乐意就此发表我的意见,谢谢。

        【讨论】:

          猜你喜欢
          • 2013-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-22
          • 2019-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多