Conversion from array to two-dimensional array using parameter

Asked

Viewed 231 times

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 a blank is found. I did this using the method split() and is already ok. However, when finding a comma, a new line should be created.

Example:

1 2, 3 4, 5 6:

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

Any hint of how to do this conversion using some parameter?

Follow the code I’ve made 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 a matrix. The column break parameter is a space, and for line break is a comma. So, if I read the string:

A B C, E F G

The new matrix needs to store:

|A|B|C|,

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

I need to get this data because I need to read 2 matrices and calculate their multiplication, but unfortunately I am not able to continue.

  • 1

    Edit the question and add what you’ve done so far, so it makes it easy to suggest something already based on your code.

  • Okay, I’ve made the change!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.