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.

96 lines
3.4 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 Textfield.
  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 2D-Array to String in order 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. }
  29. displayableString += "\n\n";
  30. }
  31. return displayableString;
  32. }
  33. /**
  34. * Checks if the Input is Valid, with Regex. Returns true if the Matrix can be
  35. * matched by the regular Expression.
  36. *
  37. * @param matrix It is the InputMatrix
  38. * @return True if the Matrix is valid Input.
  39. */
  40. protected void checkInput(String matrix) throws IllegalArgumentException {
  41. if (matrix.length() == 0) {
  42. throw new IllegalArgumentException("Please insert a Matrix");
  43. }
  44. // Matches digits witch following spaces 1 to 3 times
  45. String row1 = "(\\d*\\u0020*){1,3}";
  46. // Matches newlineCurrierReturn followed by digits witch following spaces 1 to
  47. // 3 times
  48. String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
  49. String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
  50. // TODO for verion 2 get the input check more stricktly missing matrix slots are allowed.
  51. Pattern p = Pattern.compile(row1 + row2 + row3);
  52. Matcher m = p.matcher(matrix);
  53. if(!m.matches()){
  54. throw new IllegalArgumentException("A Matrix is not in the right format, Matrix ");
  55. }
  56. }
  57. /**
  58. * Converts a String form the Inputfield to an 2D-Array aka Matrix.
  59. *
  60. * @param stringMatrix The String form the Inputfield
  61. * @return Matrix as a 2D-Array
  62. */
  63. public double[][] stringToMatrix(String stringMatrix) {
  64. ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
  65. // Splitting the strings into their rows
  66. String[] singleNumbers = null;
  67. String[] rows = stringMatrix.split("\n");
  68. for (int i = 0; i < rows.length; i++) {
  69. // Splitting rows into their Numbers
  70. singleNumbers = rows[i].split("\\s");
  71. singleNumbersArr.add(singleNumbers);
  72. }
  73. int rowlength = singleNumbersArr.get(0).length;
  74. int columCount = singleNumbersArr.size();
  75. double[][] matrix = new double[columCount][rowlength];
  76. for (int columIndex = 0; columIndex < columCount; columIndex++) {
  77. for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
  78. matrix[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
  79. }
  80. }
  81. return matrix;
  82. }
  83. }