3

I'm writing a program to take a sequence of integers from console, e.g.

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. Is there any way that we don't have to type a non-integer character to terminate the Scanner?

wei2912
  • 6,141
  • 2
  • 19
  • 20
user3681996
  • 51
  • 1
  • 6
  • possible duplicate of [How to terminate Scanner when input is complete?](http://stackoverflow.com/questions/16206813/how-to-terminate-scanner-when-input-is-complete) – Basilevs Sep 06 '14 at 15:05
  • @user3681996 please specify what you want the user to do to terminate the program ? You wan't him to enter "end" for example ? or maybe a sentinel value 99999? – Jean-François Savard Sep 06 '14 at 15:12
  • @Jean-FrançoisSavard that would be part of an answer in form of an explanation about why this can't just work when using `Scanner`. – Luiggi Mendoza Sep 06 '14 at 15:13
  • @Jean-François Savard The user types integer sequence and press enter to terminate the program – user3681996 Sep 06 '14 at 15:23

4 Answers4

3

You could get out by pressing Enter followed by Control-D.

If you don't want to do that, then there's no other way with a Scanner.

You will have to read the input by some other way, for example with a 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:

Scanner chopper = new Scanner(new Scanner(System.in).nextLine());
Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • Thanks, I got that. Put 'Scanner chopper = new Scanner(new Scanner(System.in).nextLine());' after 'System.out.println("Enter a list of number: ");' – user3681996 Sep 06 '14 at 15:34
0

Best way 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!

spb1994
  • 114
  • 8
0

Best way: Not just for this pattern, for any kind of sequence input recognition, modify delimiter of Scanner object to get required sequence recognized.

Here, in this case, change chopper delimiter to whitespace character (Spaces). i.e "\\s". You can also use "\\s*" for specifying zero or more occurrence of whitespace characters

This makes Scanner to check for spaces rather than waiting for Enter key stroke.

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

Try using a for loop.

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.
Both hasNext and next methods may block waiting for further input

velocity
  • 1,630
  • 21
  • 24
  • Why the negative point ? I tryied this example and it works perfectly. I added some exception handler and logging lines but it stays the same. int[] numCount = new int[10]; int number; for (int i=1; i<10;i++) { chopper.hasNextInt(); try{ number = chopper.nextInt(); numCount[number]++; }catch(Exception e){ System.out.println(chopper.next()); } } for (int i = 0; i < 10; i++) { System.out.println(i + " - " + numCount[i]); } } – velocity Sep 06 '14 at 15:34
  • "*I used my example and it works perfectly*" no it doesn't. This code assumes that there must be 9 integers in input so (1) it will require user to provide 9 digits, (2) it will ignore rest of numbers starting from 10th one. Instead of `for (int i:1; i<10;i++)` you may want to use `while (scanner.hasNextInt())`. – Pshemo Sep 06 '14 at 15:45