Alex Bundin
Alex Bundin

Reputation: 3

Java string split numbers

I have some rows of numbers in String str;
Numbers split by TAB

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
\\... many others, ~ 2 000 strings

I need split columns with

1 numbers to massive1,
2 numbers to massive2,
3 numbers to massive3,
4 numbers to massive4,
5 numbers to massive5,
6 numbers to massive6

I know how to solve this task with many for / while loops but I need compact solve for this task. Maybe I need Patern.compile or String.split?

Some of my code:

for (i = 0; i <= fileLen; i++) {
  while (s.charAt(i) != 13 && s.charAt(i + 1) != 10) {

    while (s.charAt(i) != 9) {
      n1.add((int) s.charAt(i));
      i++;
    }
    // skip TAB
    i++;
    // next for other column

Upvotes: 0

Views: 340

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

Instead of variables massive1, ..., massive6 a variable lenght List massives would be more suitable:

List<List<Integer>> massives = Arrays.stream(str.split("\\R"))  // Stream<String>
    .map(line -> Arrays.stream(line.split("\t"))                  //   Stream<String>
         .map(field -> Integer::valueOf)                          //   Stream<Integer>
         .collect(Collectors.toList()))                           //   List<Integer>
     .collect(Collectors.toList());                             // List<List<Integer>>

List<int[]> massives = Arrays.stream(str.split("\\R"))
     .map(line -> Arrays.stream(line.split("\t"))
         .mapToInt(Integer::parseInt)
         .toArray())
     .collect(Collectors.toList());

Maybe with:

massive1 = massives.get(0);
massive2 = massives.get(1);
massive3 = massives.get(2);
massive4 = massives.get(3);
...

Explanation:

  • String[] String#split(String regex) would split using the line-break match (\\R) into several lines.
  • Stream.of / Arrays.stream turns String[] into Stream<String> a kind of iteration through every String.
  • Stream.map turns turns every String when using Integer::valueOf into an Integer.
  • Stream.collect collects every Integer into a List.

Streams are very expressive, but might not be suited for total beginners, as they combine all into one single expression, which may easily cause errors.


After understanding the question:

int[][] rows = Stream.of(str.split("\\R"))
    .map(line -> Stream.of(line.split("\\s+"))
        .mapToInt(Integer.parseInt)
        .toArray())
    .toArray(int[][]::new);

However one wants the columns:

int m = rows.length;
int n = Arrays.stream(rows).mapToInt(line -> line.length).min().orElse(0);

int[][] columns = IntStream.range(0, n)
    .mapToObj(j -> IntStream.range(0, m)
        .map(i -> rows[i][j])
        .toArray()).toArray(int[][]::new);

System.out.println(Arrays.deepToString(columns));

Actually I advise to convert rows to columns by classical for-loops; much better readable.

But a StackOverflow answer should not necessarily pick the easiest way.

Upvotes: 1

Siju Antony
Siju Antony

Reputation: 59

Here you go,

    String str = "1 2 3 4 5 6 \n 1 2 3 4 5 6 \n 1 2 3 4 5 6";
    Arrays.asList(str.split("\n")).stream().map((s) -> s.split(" ")).map((splitBySpace) -> {
        for (String sp : splitBySpace) {
                System.out.print(sp);
            }
            return splitBySpace;
        }).forEachOrdered((String[] _item) -> {
            System.out.println();
    });

---Output---
123456
123456
123456

Upvotes: 1

Related Questions