1

I have some problem when I ask the user to input some numbers and then I want to process them. Look at the code below please.

To make this program works properly I need to input two commas at the end and then it's ok. If I dont put 2 commas at the and then program doesnt want to finish or I get an error.

Can anyone help me with this? What should I do not to input those commas at the end

package com.kurs;

import java.util.Scanner;

public class NumberFromUser {

    public static void main(String[] args) {
        String gd = "4,5,  6, 85";
        Scanner s = new Scanner(System.in).useDelimiter(", *");
        System.out.println("Input some numbers");
        System.out.println("delimiter to; " + s.delimiter());
        int sum = 0;
        while (s.hasNextInt()) {

            int d = s.nextInt();

            sum = sum + d;

        }
        System.out.println(sum);
        s.close();
        System.exit(0);

    }

}
azurefrog
  • 10,785
  • 7
  • 42
  • 56
EdXX
  • 872
  • 1
  • 14
  • 32
  • in your loop while you can go for ever no limite those tow commas force you to exit from the loop you should have some limite of the whil loop or some thing to exit from loop – Abdelhak Nov 15 '15 at 20:45

4 Answers4

3

Your program hangs in s.hasNextInt().

From the documentation of Scanner class:

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input.

In a few words, scanner is simply waiting for more input after the last integer, cause it needs to find your delimiter in the form of the regular expression ", *" to decide that the last integer is fully typed.

You can read more about your problem in this discussion:

Link to the discussion on stackoverflow

To solve such problem, you may change your program to read the whole input string and then split it with String.split() method. Try to use something like this:

import java.util.Scanner;

public class NumberFromUser {

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] tokens = sc.nextLine().split(", *");

        int sum = 0;

        for (String token : tokens) {
            sum += Integer.valueOf(token);
        }

        System.out.println(sum);
    }

}
Community
  • 1
  • 1
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
0

look you can do some thing like this mmm.

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Input some numbers");
        System.out.println("When did you to finish and get the total sum enter ,, and go");
        boolean flag = true;
        int sum = 0;
        while (s.hasNextInt() && flag) {

            int d = s.nextInt();
            sum = sum + d;
        }

        System.out.println(sum);

    }
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

Try allowing end of line to be a delimiter too:

Scanner s = new Scanner(System.in).useDelimiter(", *|[\r\n]+");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

I changed your solution a bit and probably mine isn't the best one, but it seems to work:

    Scanner s = new Scanner(System.in);
    System.out.println("Input some numbers");
    int sum = 0;
    if (s.hasNextLine()) {
        // Remove all blank spaces
        final String line = s.nextLine().replaceAll("\\s","");

        // split into a list
        final List<String> listNumbers = Arrays.asList(line.split(","));

        for (String str : listNumbers) {
            if (str != null && !str.equals(""))  {
            final Integer number = Integer.parseInt(str);
            sum = sum + number;
            }
        }
    }
    System.out.println(sum);
Aimert
  • 146
  • 6
  • Thanks for the code but I would like to know how to do this using "," delimeter and while loop – EdXX Nov 15 '15 at 21:07