简体   繁体   English

使用具有整数序列的扫描程序

[英]Using Scanner with sequence of Integers

I'm writing a program to take a sequence of integers from console, eg 我正在编写一个程序来从控制台获取一系列整数,例如

1 5 3 4 5 5 5 4 3 2 5 5 5 3

then compute the number of occurrences and print the following output: 然后计算出现次数并打印以下输出:

0 - 0
1 - 1
2 - 1
3 - 3
4 - 2
5 - 7
6 - 0
7 - 0
8 - 0
9 - 0

where the second number is the number of occurrences of the first number. 其中第二个数字是第一个数字的出现次数。

Code: 码:

public static void main (String args[])
{
    Scanner chopper = new Scanner(System.in);
    System.out.println("Enter a list of number: ");

    int[] numCount = new int[10];
    int number;

    while (chopper.hasNextInt()) {
        number = chopper.nextInt();
        numCount[number]++;
    }

    for (int i = 0; i < 10; i++) {
        System.out.println(i + " - " + numCount[i]);
    }
}

But after inputing the sequence, we must type a non-integer character and press "Enter" to terminate the Scanner and execute the "for" loop. 但输入序列后,我们必须输入一个非整数字符,然后按“Enter”键终止扫描仪并执行“for”循环。 Is there any way that we don't have to type a non-integer character to terminate the Scanner? 有没有办法我们不必键入非整数字符来终止扫描仪?

You could get out by pressing Enter followed by Control-D. 按Enter键然后按Control-D可以退出。

If you don't want to do that, then there's no other way with a Scanner . 如果您不想这样做,那么Scanner就没有别的办法了。

You will have to read the input by some other way, for example with a BufferedReader : 您必须通过其他方式读取输入,例如使用BufferedReader

String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
Scanner chopper = new Scanner(line);

Even better (inspired by @user3512478 's approach), with two Scanners, without BufferedReader : 更好(灵感来自@ user3512478的方法),有两个扫描仪,没有BufferedReader

Scanner chopper = new Scanner(new Scanner(System.in).nextLine());

Best way IMO: 最好的方式IMO:

String str;
Scanner readIn = new Scanner(System.in);
str = readIn.nextLine();
String[] nums = str.split(" ");
int[] finalArray = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
    finalArray[i] = Integer.parseInt(nums[i]);
return finalArray;

Hope this helps! 希望这可以帮助!

Best way: Not just for this pattern, for any kind of sequence input recognition, modify delimiter of Scanner object to get required sequence recognized. 最佳方式:不仅适用于此模式,对于任何类型的序列输入识别,修改Scanner对象的分隔符以获得所需的序列识别。

Here, in this case, change chopper delimiter to whitespace character (Spaces). 在这种情况下,将斩波器分隔符更改为空白字符(空格)。 ie "\\\\s" . "\\\\s" You can also use "\\\\s*" for specifying zero or more occurrence of whitespace characters 您还可以使用"\\\\s*"指定零个或多个空格字符

This makes Scanner to check for spaces rather than waiting for Enter key stroke. 这使扫描仪检查空格而不是等待Enter键击。

public static void main (String args[])
{
    Scanner chopper = new Scanner(System.in).useDelimiter("\\s"); \\ Delimiter changed to whitespace.
    System.out.println("Enter a list of number: ");

    int[] numCount = new int[10];
    int number;

    while (chopper.hasNextInt()) {
        number = chopper.nextInt();
        numCount[number]++;
    }

    for (int i = 0; i < 10; i++) {
        System.out.println(i + " - " + numCount[i]);
    }
}

Try using a for loop. 尝试使用for循环。

for (int i:1; i<10;i++) {
     chopper.hasNextInt()
     number = chopper.nextInt();
     numCount[number]++;
}

From Oracle Doc it says: A scanning operation may block waiting for input. 从Oracle Doc中可以看出:扫描操作可能会阻止等待输入。
Both hasNext and next methods may block waiting for further input hasNext和next方法都可能阻止等待进一步输入

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

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