简体   繁体   English

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

[英]unable to take all the inputs into string array in java

I have written a code to take string inputs and store in a string array in java.我编写了一个代码来获取字符串输入并存储在 java 中的字符串数组中。 My problem is that when I declare size of string array as 3 it has to take three strings into string array but it only takes two string into the string array and doesn't accept the third one.我的问题是,当我将字符串数组的大小声明为 3 时,它必须将三个字符串放入字符串数组中,但它只将两个字符串放入字符串数组中,并且不接受第三个。 Can anyone help me to solve the problem?谁能帮我解决问题?

Code代码

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]); 
        }
        }
}

output输出

enter length of string
3
enter string values
one
two

one
two

You have to add sc.nextLine() before start reading input string from console.在开始从控制台读取输入字符串之前,您必须添加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]); 
        }
    }
}

This would give you the desired results.这会给你想要的结果。

input:输入:

enter length of string
3
enter string values
one
two
three

output:输出:

one
two
three

When you read an int and the want to read a full String , then you have to move a pointer inside Scanner to the next line.当您读取一个int并且想要读取一个完整的String ,您必须将Scanner内的指针移动到下一行。 PS It is easy to find if you debug you program. PS如果您调试程序,很容易找到。

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]);
}

Output:输出:

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

I would suggest to take a look at this code, also, try not to use value names such as n, it could really improve readability and help you to see the problem.我建议看一下这段代码,同时尽量不要使用 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]); 
    }
  }
}

To get your desired process, please simply change a[i]=sc.nextLine();要获得您想要的流程,只需更改a[i]=sc.nextLine(); to a[i]=sc.next();a[i]=sc.next(); .Unless your string input has spaces in them, this should work fine, else place sc.nextLine(); .除非您的字符串输入中有空格,否则这应该可以正常工作,否则放置sc.nextLine(); before your for loop在你的 for 循环之前

this is how I see the problem;这就是我看待问题的方式;

nextInt(); expects and stores the first word of input as int , else an error is thrown, however, it doesn't break out of the current line.期望并将输入的第一个单词存储为int ,否则会引发错误,但是,它不会跳出当前行。

So when you run n=sc.nextInt();所以当你运行n=sc.nextInt(); and gave the input并给出了输入

enter length of string
3

3 was saved to n but the system cursor still stays behind it like so, 3| 3被保存到n但系统光标仍然像这样停留在它后面, 3| , ,

NB: the output stream is different from the input stream, so it doesn't matter that, System.out.println("enter string values");注意:输出流与输入流不同,所以System.out.println("enter string values"); is run, the cursor for the input stream still stays behind 3;运行时,输入流的光标仍然停留在 3 后面;

enter length of string
3|
enter string values

a[i]=sc.nextLine() expects and stores the sentence after the cursor, else it breaks to the next line. a[i]=sc.nextLine()期望并存储光标后的句子,否则它会中断到下一行。 But if the cursor is already on a new line, it gives the chance to enter a new sentence (then stores it if you want) and moves over to a new line.但是如果光标已经在一个新行上,它就有机会输入一个新句子(如果需要,然后存储它)并移动到一个新行。

so as there is no sentence after the cursor 3|所以光标3|后面没有句子, it breaks to the next line and assigns the break line (newline) command "\\n" to a[0] in the for loop when i = 0 , ,当i = 0时,它会中断到下一行并将中断行(换行)命令"\\n"分配a[0] for 循环中的a[0]

so, since the cursor is now on a new line, it gives the option to enter a new sentence and stores it to a[1] when enter is tapped for i = 1 and moves to the next line, the same process is executed for when i = 2因此,由于光标现在位于新行上,因此它提供了输入新句子的选项并将其存储到a[1]i = 1时轻按 enter 并移动到下一行,执行相同的过程当i = 2

then the contents of String[] a is displayed as follows;那么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

I had fun giving my opinion on this, Thank You.我很高兴就此发表我的意见,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM