From 023b0dfe3858af9204b132c19c91506c39aecbad Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Sun, 14 Jul 2019 19:08:16 +0200 Subject: [PATCH] String to matrix function is finished. --- .../com/ugsbo/matrixcalc/MatrixCalcMath.java | 11 +++-- .../ugsbo/matrixcalc/StringToMatrixTest.java | 47 ++++++++++++++++++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java index bae9733..98bb194 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java @@ -232,20 +232,21 @@ public class MatrixCalcMath { ArrayList singleNumbersArr = new ArrayList(); - //Splitting the strings into thier rows + //Splitting the strings into their rows String[] singleNumbers = null; String[] rows = stringMatrix.split("\n"); for (int i = 0; i < rows.length; i++) { - System.out.println(rows[i]); + //System.out.println(rows[i]); + //Splitting rows into their Numbers singleNumbers = rows[i].split("\\s"); singleNumbersArr.add(singleNumbers); } - int rowCount = rows.length; //row.length + int rowlength = singleNumbersArr.get(0).length; //row.length int columCount = singleNumbersArr.size(); //output.length - double[][] result = new double[columCount][rowCount]; + double[][] result = new double[columCount][rowlength]; - for (int columIndex = 0; columIndex < singleNumbersArr.size(); columIndex++) { + for (int columIndex = 0; columIndex < columCount; columIndex++) { for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) { result[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]); } diff --git a/src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java b/src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java index 104340d..2e5934d 100644 --- a/src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/StringToMatrixTest.java @@ -36,6 +36,51 @@ public class StringToMatrixTest { double[][] result = matrixMath.stringToMatrix(inputString); assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1); - assertArrayEquals("The first row is not correct", expected[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1); + } + + @Test + public void StringWithSixDigitNumbersToMatrix_Returns1by6Matrix() { + String inputString = "1 2 3 4 5 6"; + double[][] expected = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } }; + + double[][] result = matrixMath.stringToMatrix(inputString); + + assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1); + } + + @Test + public void StringWithSixDigitNumbersToMatrix_Returns2by3Matrix() { + String inputString = "1 2 3\n4 5 6"; + double[][] expected = { { 1.0, 2.0, 3.0}, { 4.0, 5.0, 6.0 } }; + + double[][] result = matrixMath.stringToMatrix(inputString); + + assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1); + assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1); + } + + @Test + public void StringWithSixDigitNumbersToMatrix_Returns3by2Matrix() { + String inputString = "1 2\n3 4\n5 6"; + double[][] expected = { { 1.0, 2.0}, { 3.0, 4.0}, { 5.0, 6.0 } }; + + double[][] result = matrixMath.stringToMatrix(inputString); + + assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1); + assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1); + assertArrayEquals("The therd row is not correct", expected[2], result[2], 0.1); + } + + @Test + public void StringWithNineDigitNumbersToMatrix_Returns3by3Matrix() { + String inputString = "1 2 3\n4 5 6\n7 8 9"; + double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } }; + + double[][] result = matrixMath.stringToMatrix(inputString); + + assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1); + assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1); + assertArrayEquals("The thierd row is not correct", expected[2], result[2], 0.1); } }