如何使用 Java 中的 Scanner 将输入值存储在数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18888476/
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-12 11:56:25  来源:igfitidea点击:

How to store input values in an array by using Scanner in Java

javaarrays

提问by user2709885

Hi I am new to Java and I was experimenting with the Scanner class. I am trying to figure out a small problem in which I want to enter two inputs such as: 4 5 6 and 8 9 0. I want to store 4,5,6 in one array and 8,9,0 in another array and then print these arrays. But I am unable to do so. I wrote the following code :

嗨,我是 Java 新手,我正在尝试使用 Scanner 类。我想找出一个小问题,我想输入两个输入,例如:4 5 6 和 8 9 0。我想将 4,5,6 存储在一个数组中,将 8,9,0 存储在另一个数组中,然后然后打印这些数组。但我无法这样做。我写了以下代码:

public class scanner {

public static void main(String[] args) {

    int[] array = new int[3];
    int[] array2 = new int[3];
    Scanner scan = new Scanner(System.in);

    int i = 0;
    while(scan.hasNextInt()){
        array[i] = scan.nextInt();
        i++;
        if(i == 3){
            break;
        }
    }

    i = 0;
    while(scan.hasNextInt()){
        array2[i] = scan.nextInt();
        i++;
        if(i == 3){
            break;
        }
    }

    for(int j  = 0; j < array.length; j++){
        System.out.println(array[j]);
    }

    for(int j  = 0; j < array2.length; j++){
        System.out.println(array2[j]);
    }
}

}

But this doesn't takes the input 4 5 6 in one single line. I want to enter 4 5 6 in one line so that all the three digits are stored in the array. Can someone please help me. I assume I should use delimiter to remove the white space but I am not sure how to go about it.

但这不会在一行中输入 4 5 6 。我想在一行中输入 4 5 6 以便所有三个数字都存储在数组中。有人可以帮帮我吗。我假设我应该使用分隔符来删除空格,但我不知道如何去做。

回答by SudoRahul

You can try something like this, instead of the 2 whileloops you've to populate your arrays.

您可以尝试这样的操作,而不是while您必须填充数组的 2 个循环。

Here the scanner reads line by line and each line is split on space(as you mentioned in your question) and then each splitted element is converted to an integer and the array is populated.

在这里,扫描仪逐行读取,每行在空间上分割(如您在问题中提到的),然后每个分割的元素都转换为整数并填充数组。

String line1 = scan.nextLine(); // Read 1st line
String[] numbers1 = line1.split(" "); // Split based on space
for(int i=0;i<numbers1.length;i++){
    array[i] = Integer.parseInt(numbers1[i]);
}

String line2 = scan.nextLine(); // Read 2nd line
String[] numbers2 = line2.split(" "); // Split based on space
for(int i=0;i<numbers2.length;i++){
    array2[i] = Integer.parseInt(numbers2[i]);
}

Sample I/O:-

示例输入/输出:-

Input:
1 2 3
4 5 6

Output:
1
2
3
4
5
6

回答by MaVRoSCy

To do this you have to get the String as a hole 4 5 6and use split(" ")to get an array:

为此,您必须将 String 作为一个孔4 5 6并使用split(" ")来获取一个数组:

String val="4 5 6";
String [] arr = val.split(" ");

Then go on and loop your arry as you do now

然后继续像现在一样循环你的 arry

回答by AppX

Is this what you are looking for?

这是你想要的?

public class InputScanner {

    public static void main(String[] args) {

        int[] array = new int[3];
        int[] array2 = new int[3];
        Scanner scan = new Scanner(System.in);

        int i = 0;
        while (scan.hasNextInt()) {
            array[i] = scan.nextInt();
            i++;
            if (i == 3) {
                break;
            }
        }

        i = 0;
        while (scan.hasNextInt()) {
            array2[i] = scan.nextInt();
            i++;
            if (i == 3) {
                break;
            }
        }

        for (int j = 0; j < array.length; j++) {
            System.out.print("" + array[j] + (j<(array.length-1)?" ":"\n"));
        }

        for (int j = 0; j < array2.length; j++) {
            System.out.print("" + array2[j] + (j<(array2.length-1)?" ":"\n"));
        }
    }

}

回答by Matej

Your code is ok. The default delimiter for Scanner is whitespace according to javadoc.

你的代码没问题。根据 javadoc,扫描仪的默认分隔符是空格。

The input is broken into tokens by the delimiter pattern, which is whitespace by default

输入被分隔符模式分解为标记,默认情况下是空格

What probably confuse you is that the print put each integer in a new line. To fix that you can simply write this:

可能让您感到困惑的是,打印将每个整数放在一个新行中。要解决这个问题,你可以简单地写这个:

        for(int j  = 0; j < array.length; j++){
            System.out.print(array[j] + " ");
        }
        System.out.println();
        for(int j  = 0; j < array2.length; j++){
            System.out.print(array2[j] + " ");
        }

回答by Amrit Kumar Muduli

int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);

for(int i = 0 ; i < array.length ; i++){
    array[i] = scan.nextInt();
}

for(int i = 0 ; i < array2.length ; i++){
    array2[i] = scan.nextInt();
}

for(int j  = 0; j < array.length; j++){
    System.out.println(array[j]);
}

for(int j  = 0; j < array2.length; j++){
    System.out.println(array2[j]);

回答by Bharat Wadhawan

import java.util.*;
class Ab
{
    public static void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        String str=s.nextLine();
        String [] arr=str.split(" ");
        Integer [] a=new Integer[arr.length];
        for(int i=0;i<arr.length;i++)
        {
            a[i]=Integer.parseInt(arr[i]);
        }
        for(int i=0;i<a.length;i++)
        {
            System.out.println(a[i]);
        }
    }
}

You may try this out and can make changes accordingly.

您可以尝试一下,并可以进行相应的更改。