0

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
9
  • Can you post the code you have so far? Commented May 23, 2019 at 12:36
  • what do you mean by 1 numbers to massive1 ? Commented May 23, 2019 at 12:42
  • for (i=0; i<=fileLen; i++){ while ( s.charAt(i) != 13 && s.charAt(i+1) != 10) { // EOS while (s.charAt(i) != 9) // TAB { n1.add((int)s.charAt(i)); i++; } // skip TAB i++; Commented May 23, 2019 at 12:43
  • 2
    Expected output may help to explain what you need. Commented May 23, 2019 at 12:45
  • @Hades: <code>I need, 1 all column store into massive1: 1 . . . . . 1 . . . . . 1 . . . . . Next need, 2 column store into massive2: . 2 . . . . . 2 . . . . . 2 . . . . Next need, 3 column store into massive3: . . 3 . . . . . 3 . . . . . 3 . . . Next need, 4 column store into massive4: . . . 4 . . . . . 4 . . . . . 4 . . Next need, 5 column store into massive5: . . . . 5 . . . . . 5 . . . . . 5 . Next need, 6 column store into massive6: . . . . . 6 . . . . . 6 . . . . . 6 . . . . . 6<code> Commented May 23, 2019 at 12:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry for delay in answer. I have errors when I'm compile this code: ~~Error:(82, 33) java: incompatible types: cannot infer type-variable(s) R (argument mismatch; bad return type in lambda expression java.lang.Object is not a functional interface) Error:(84, 29) java: incompatible types: inference variable T has incompatible bounds equality constraints: java.util.List<java.lang.Integer>~~
field -> Integer::parseInt should have been either field -> Integer.parseInt(field) or the correction in the answer.
List<List<Integer>> massives = Arrays.stream(str.split("\\R")) .map(line -> Arrays.stream(line.split("\t")) .map(field -> Integer.parseInt(field)) .collect(Collectors.toList())) .collect(Collectors.toList()); // out [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6] // I need out [[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2]
Joop Eggen: Big thanks! its code work for me. I'm master of intelligible explanation(no)
1

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

1 Comment

Must be: mas1 = "111"; mas2="222"; mas="333"; ... mas1 - its first column, mas2=second column; In your example row(without spaces) not column. Sorry for bad explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.