Array conversion to two-dimensional array using parameter

2

I have a question regarding the conversion of vectors (array) to two-dimensional vectors.

I need to read a string and separate the columns when there is a blank space. I did this using the split() method and it's already ok. However, when you find a comma, a new line must be created.

Example:

1 2, 3 4, 5 6:

[1, 2,
3, 4,
5, 6]

Any tips on how to do this using some parameter?

Here is the code I've done so far:

    Scanner scan = new Scanner(System.in);

    String vetorEnt;
    System.out.println("Informe a primeira matriz, com base no seguinte exemplo:");
    vetorEnt = scan.nextLine();
    String[] vetor1 = vetorEnt.split(",");
    System.out.println("Vetor quebrado" + Arrays.toString(vetor1));

    String[][] matriz1 = new String[vetor1.length][];

    for (int i = 0; i < matriz1.length; i++) {
      matriz1[i] = vetor1[i].split("\s");
    }

    for (int i = 0; i < matriz1.length; i++) {
      for (int j = 0; j < matriz1[i].length; j++) {
        System.out.print(matriz1[i][j] + "");
      }
      System.out.println();
    }
    System.out.println(matriz1.length);
    System.out.println(vetor1.length);
  }

What happens: I need to read a string and transform it into an array. The parameter for column break is a space, and for line break is a comma. So if I read the string:

A B C, E F G

The new array needs to be stored:

|A|B|C|,

|E|F|G|. -> Matriz 2x3

I need to get this data because I need to read 2 arrays and calculate the multiplication of them, but unfortunately I can not go on.

    
asked by anonymous 19.08.2018 / 16:09

1 answer

0
Hello, well I was testing the code you showed and it seems to me that everything is ok, the only irregularity I found was when it was time to display the dimensions of the matrix, you kind of are displaying the number of lines twice, since the value of vetor1.length is the same as that used to declare the matrix new String[vetor1.length][] , I decided to get the length of the first item of the array, thus:

System.out.println(matriz1.length+" X "+matriz1[0].length);

That was the problem?, sorry if I did not quite understand your doubt. I hope I have helped;)

    
19.08.2018 / 20:32