开发者

if i entered numbers separated with commas, how could i extract the numbers? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.

Closed 8 years ago.

Improve this question 开发者_StackOverflow中文版

for example....

26, 15, 37

how could i get the numbers from a Scanner , ( lets say for instance i want to add or subtract,,,?)


Take a look at String.split().

If you want to use the Scanner API:

private static final Pattern COMMA_PATTERN = Pattern.compile("\\s*,\\s*");

public List<Integer> getIntegerList() {
    // Assumes scanner is positioned at first integer in list.
    List<Integer> integers = new ArrayList<Integer>();
    for (;;) {
        integers.add(scanner.nextInt());
        if (scanner.hasNext(COMMA_PATTERN)) {
            // Read and discard comma token, and continue parsing list.
            scanner.next();
        } else {
            // Number is not followed by comma, stop parsing.
            break;
        }
    }
    return integers;
}

More error handling is needed, but hopefully, this example illustrates the approach.

You can also use Scanner.useDelimiter():

private static final Pattern COMMA_PATTERN = Pattern.compile("\\s*,\\s*");

public List<Integer> getIntegerList() {
    // Assumes scanner is positioned at first integer in list.
    List<Integer> integers = new ArrayList<Integer>();
    Pattern oldDelimiter = scanner.delimiter();
    scanner.useDelimiter(COMMA_PATTERN);
    while (scanner.hasNextInt()) {
        integers.add(scanner.nextInt());
    }
    // Reset delimiter
    scanner.useDelimiter(oldDelimiter);
    return integers;
}


Use Scanner.useDelimiter. It actually takes regex, so you'd want to learn some basics.

    String text = "1 , 2 3,      4,5";
    Scanner sc = new Scanner(text).useDelimiter("\\s*,?\\s*");
    while (sc.hasNextInt()) {
        System.out.println(sc.nextInt());
    } // prints "1", "2", "3", "4","5"

See also

  • http://www.regular-expressions.info/ -- the best tutorial resource

Related questions

  • How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
    • Using hasNextInt() to prevent exception is much better than Integer.parseInt and catch NumberFormatException


String.split() is OK, but StringTokenizer works everywhere and in every version of Java.

StringTokenizer st = new StringTokenizer("26, 15, 37", ", ");
int sum = 0;
while (st.hasMoreTokens()) {
    sum += Integer.parseInt(st.nextToken());
}


Try to set a delimiter for your scanner object:

Scanner s = new Scanner(System.in).useDelimiter(", *");
int first = s.nextInt();
int second = s.nextInt();
...

More examples can be found in Scanner documentation.


Look at useDelimiter. You need a regex that will match either whitespace or commas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜