Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

  1. package com.ugsbo.matrixcalc;
  2. import java.util.ArrayList;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class MatrixCalcIOUtils {
  6. /**
  7. * Prints a given 2D-Array to the output text Field.
  8. *
  9. * @param output2DArray The Array that gets Displayed
  10. */
  11. protected String outputMatrixToOutputText(double[][] output2DArray) {
  12. // convert array to String
  13. String DisplayableString = convertsArrayToStringInOrderToDisplayIt(output2DArray);
  14. // Display output
  15. return DisplayableString;
  16. }
  17. /**
  18. * Converts Array to String in oder to Display it.
  19. *
  20. * @param array2D the array wich will be converted to an Displayable String
  21. * @return The Displayable String
  22. */
  23. protected String convertsArrayToStringInOrderToDisplayIt(double[][] array2D) {
  24. String displayableString = "";
  25. for (int i = 0; i < array2D.length; i++) {
  26. for (int j = 0; j < array2D[0].length; j++) {
  27. displayableString += array2D[i][j] + " ";
  28. // System.out.println(result[i][j]);
  29. }
  30. displayableString += "\n\n";
  31. }
  32. return displayableString;
  33. }
  34. /**
  35. * Chcks if the Input is Valid, with Regex. Returns true if the Matrix can be
  36. * matched by the regular Expression.
  37. *
  38. * @param matrix It is the InputMatrix
  39. * @return true if the Matrix is valid Input.
  40. */
  41. protected boolean checkInput(String matrix) throws IllegalArgumentException {
  42. boolean isMatched = false;
  43. if (matrix.length() == 0) {
  44. throw new IllegalArgumentException("Please insert a Matrix");
  45. }
  46. // Matches digits witch following spaces 1 to 3 times
  47. String row1 = "(\\d*\\u0020*){1,3}";
  48. // Matches newlineCurrierReturn followed by digits witch following spaces 1 to
  49. // 3times
  50. String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
  51. String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
  52. // TODO get the input check more stricktly missing matrix slots are allowed.
  53. Pattern p = Pattern.compile(row1 + row2 + row3);
  54. Matcher m = p.matcher(matrix);
  55. isMatched = m.matches();
  56. // TODO change the funktion to void and just throw exceptions if something went
  57. // worng
  58. // System.out.println(isMatched);
  59. return isMatched;
  60. }
  61. /**
  62. * Converts a String form the Inputfield to an 2D-Array aka Matrix.
  63. *
  64. * @param stringMatrix The String form the Inputfield
  65. * @return Matrix as a 2D-Array
  66. */
  67. public double[][] stringToMatrix(String stringMatrix) {
  68. ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
  69. // Splitting the strings into their rows
  70. String[] singleNumbers = null;
  71. String[] rows = stringMatrix.split("\n");
  72. for (int i = 0; i < rows.length; i++) {
  73. // System.out.println(rows[i]);
  74. // Splitting rows into their Numbers
  75. singleNumbers = rows[i].split("\\s");
  76. singleNumbersArr.add(singleNumbers);
  77. }
  78. int rowlength = singleNumbersArr.get(0).length; // row.length
  79. int columCount = singleNumbersArr.size(); // output.length
  80. double[][] matrix = new double[columCount][rowlength];
  81. for (int columIndex = 0; columIndex < columCount; columIndex++) {
  82. for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
  83. matrix[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
  84. }
  85. }
  86. return matrix;
  87. }
  88. }