String a = "0 2 4 1 4.5 2.2 1.1 4.3 5.2";
Here we have a String of 9 numbers, some of which are double, and some of which are int. I need to convert this into a matrix and when I am given only integer values in the String, it is fairly easy because all I do cast double on a whole integer after I parseInt for each String value that is a digit.
For instance,
String a = "1 2 3 4 5 6 7 8 9";
double[][] matrixA = new double[3][3];
int counter = 0;
for (int i = 0; i <= a.length(); i++) {
char c = a.charAt(i);
if (c == ' ') {
counter = counter;
} else {
counter++;
double k = Double.parseDouble(String.valueOf(c));
if (counter <= 3) {
matrixA[0][counter - 1] = k;
} else if (counter > 3 && counter <= 6) {
matrixA[1][counter - 4] = k;
} else {
matrixA[2][counter - 7] = k;
}
}
}
This gives me a 3-by-3 matrix that goes from 1-9. However, when I am given a string that contains real numbers or 'doubles' my method fails to create a matrix because it counts each element of the string, one of which is a period. So how can I create a 3-by-3 matrix when the String that is given to me contains a double?