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.