How to extract multiple integers from a String in Java?

16,543

Solution 1

This is probably overly convoluted, but hay...

The first thing we need to do is remove all the crap we don't need...

String[] crap = {"(", ")", ",", ";"};
String text = "(123, 234; 345, 456) (567, 788; 899, 900)";
for (String replace : crap) {
    text = text.replace(replace, " ").trim();
}
// This replaces any multiple spaces with a single space
while (text.contains("  ")) {
    text = text.replace("  ", " ");
}

Next, we need to seperate the individual elements of the string into a more manageable form

String[] values = text.split(" ");

Next, we need to convert each String value to an int

int[] iValues = new int[values.length];
for (int index = 0; index < values.length; index++) {

    String sValue = values[index];
    iValues[index] = Integer.parseInt(values[index].trim());

}

Then we display the values...

for (int value : iValues) {
    System.out.println(value);
}

Solution 2

Strategy: Find one or more numbers that are together, through a regular expression to be added to a list.

Code:

    LinkedList<String> list = new LinkedList<>();
    Matcher matcher = Pattern.compile("\\d+").matcher("(123, 234; 345, 456) (567, 788; 899, 900)");
    while (matcher.find()) {
        list.add(matcher.group());
    }
    String[] array = list.toArray(new String[list.size()]);
    System.out.println(Arrays.toString(array));

Output:

[123, 234, 345, 456, 567, 788, 899, 900]

Solution 3

You've almost certainly seen the quote:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

But regular expressions really are your friend for this sort of thing.

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Numbers {
    public static void main(String[] args) {
        String s = "(123, 234; 345, 456) (567, 788; 899, 900)";
        Matcher m = Pattern.compile("\\d+").matcher(s);
        List<Integer> numbers = new ArrayList<Integer>();
        while(m.find()) {
            numbers.add(Integer.parseInt(m.group()));
        }
        System.out.println(numbers);
    }
}

Outputs:

[123, 234, 345, 456, 567, 788, 899, 900]

Solution 4

Iterate through each character and store numbers in a temporary array until you find a character(like ,, ;) then store the data from temporary array into your array and then empty that temporary array for next use.

Share:
16,543
子昂 陳
Author by

子昂 陳

Updated on June 27, 2022

Comments

  • 子昂 陳
    子昂 陳 almost 2 years

    I got a series of string like "(123, 234; 345, 456) (567, 788; 899, 900)". How to a extract those numbers into an array like aArray[0]=123, aArray=[234], ....aArray[8]=900;

    Thank you

  • Thobias Bergqvist
    Thobias Bergqvist over 11 years
    This is what I would do. Just one thing though. Looking at the format of the string, Mr Quoi might want a different structure. It looks like a list of lists with pairs in them. List<List<Tupel<Integer, Integer>>> maybee? Still, I would go for the regex solution.
  • Daniel De León
    Daniel De León over 11 years
    Check the element 8 of the desire array in the question. Thats give me the clue, of how the array must be. ;)
  • damzam
    damzam over 11 years
    Did someone really downvote my solution on this? Why not leave a comment to explain why?
  • Coding Mash
    Coding Mash over 11 years
    Just code answers are usually not welcome in Stack Overflow...