无法在 java 中将所有输入都放入字符串数组

unable to take all the inputs into string array in java

我编写了一段代码来获取字符串输入并将其存储在 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

在开始从控制台读取输入字符串之前,您必须添加 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

当您阅读 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

我建议看一下这段代码,另外,尽量不要使用诸如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]); 
    }
  }
}

要获得您想要的过程,请简单地将 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|之后没有语句,所以会换行到下一行,并将换行(换行)命令"\n"赋值给for循环中的a[0]i = 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

我很高兴就此发表意见,谢谢。