1

I would like to know how i could possibly create from a string a list of numbers that are side by side.

Input:

"This is a test 234 234 34 56 and possibly 34 not a good example 234,34"

I'm actually using this code that creates a list of all numbers in the string, but no separated.

public List getNumbers(String s) {
    String str = s.replaceAll("[^-?0-9]+", " ");
    return Arrays.asList(str.trim().split(" "));
}

Output i have:

["234", "234", "34", "56", "34", "234", "34"]

Output i would like to obtain:

["234 234 34 56","34","234 34"]

I really hope you guys could help me, Thank you. (I'm new here, and aren't english sorry if i'm doing something wrong )

TomyD
  • 11
  • 3
  • 1
    I'm voting to close this question as off-topic because questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. [Source](https://stackoverflow.com/help/on-topic) – Joe C May 14 '17 at 18:53
  • This is not script. This is java. Java is a real programming language, not a script. – Mike Nakis May 14 '17 at 18:55
  • Oh, i'm sorry i'm new out here :/, what should i add, it isn't for any homework, i'm actually trying to make a personal private project – TomyD May 14 '17 at 18:55
  • Mike Nakis Sorry, i'm not english, my vocabulary isn't perfect – TomyD May 14 '17 at 18:57
  • please post an [mcve] and what output it gives (as opposed to what output you want, which you have already shown.) – Mike Nakis May 14 '17 at 18:58
  • Try using this answer : http://stackoverflow.com/questions/6020384/create-array-of-regex-matches to match with [0-9]+ that will look for all numbers – Thijs Steel May 14 '17 at 18:58
  • 1
    Why `234,34` become `234 34` ? – Ashraful Islam May 14 '17 at 18:59
  • Youcould split the string by whitecharacters, and filtert only values that are numbers. – Beri May 14 '17 at 19:00
  • Also: `List getNumbers(String s)` is a style of writing java that has not been in use for the last 20 years or so. The only people who still write java like that are old professors who don't like learning anything new. So, this does smell like homework. But you say it is not homework? Fine, I'll take your word for it. For now. – Mike Nakis May 14 '17 at 19:01
  • You need to understand, at first, the logic execution on an algorithmic level. For example, when I have:1) 234 234 -> 234. (2:) 234 34 -> 234.34. Because when you later have: 56, what do you want with it? – Vasyl Lyashkevych May 14 '17 at 19:01
  • @Beri, that won't work -> "234,34" – Thijs Steel May 14 '17 at 19:01

3 Answers3

4

Here's one approach:

String str = "This is a test 234 234 34 56 and possibly 34 not a good example 234,34";
System.out.println(Arrays.stream(str.split("[^0-9 ]+"))
            .map(i -> i.trim())
            .filter(i -> !i.isEmpty())
            .collect(Collectors.toList()));

Output:

[234 234 34 56, 34, 234, 34]

Explanation:

I split the string on anything that isn't a number or a space, then trim each part. Finally, I remove empty strings and collect everything into a list.

Edit: Andy Turner's approach is nicer. I'll leave mine up though.

Community
  • 1
  • 1
Malt
  • 28,965
  • 9
  • 65
  • 105
1

Use a Matcher:

Matcher matcher = Pattern.compile("[0-9]([\\s0-9]*[0-9])?").matcher(s);
while (matcher.find()) {
  System.out.println(matcher.group());
}

Note that this treats 234,34 as two tokens; but it's not clear why that would be anyway. Please clarify in the question.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1
import java.util.List;
import java.util.Arrays;
import java.util.LinkedList;

public static void main(String[] args) {

    String input = "This is a test 234 234 34 56 and possibly 34 not a good example 234,34";
    List < String > temp = new LinkedList < String > (Arrays.asList(input.split("\\s*(,|\\s)\\s*")));
    for (int i = 0; i < temp.size(); i++) {
        if (!isNumeric(temp.get(i))) {
            System.out.println(temp.get(i));
            temp.remove(i);
            i--;
        }
    }

    System.out.println(temp);

}

public static boolean isNumeric(String str) {
    try {
        double d = Double.parseDouble(str);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}