I have this string in Java
String str = "a, b, c, d, e, f, g, h, i, j, k, l, m, , n, o, p";
And I need to convert it into a matrix where the rows and columns are specified. That is:
3 by 3
a b c
d e f
g h i
O
2 for 8
a b c d e f g i
j k l m n o p q
This is my code, which always returns null
public static String Format(String str, int rows, int columns)
{
try
{
String[][] matrix = new String[rows][columns];
String[][] arr = Arrays.stream(str.substring(2, str.length() - 2).split("\],\["))
.map(e -> Arrays.stream(e.split("\s*,\s*"))
.toArray(String[]::new)).toArray(String[][]::new);
String append = "|\t", result;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
matrix[i][j] = arr[i][j];
}
}
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
append += matrix[i][j] + "\t";
}}
result = append + "|";
append = "|\t";
return result;
}
catch(Exception e)
{
return null;
}
}