Browse Source

Added try catch to DetA and subtraction inOrder to Display the reason why the input is Invalid

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
197721b332
  1. 58
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java
  2. 4
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java

58
src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

@ -93,16 +93,47 @@ public class MatrixCalcController {
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) {
boolean stop = false;
try {
util.checkInput(stringMatrixA);
} catch (Exception e) {
stop = true;
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
try {
util.checkInput(stringMatrixB);
} catch (Exception e) {
stop = true;
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB) && !stop) {
double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB);
try {
double[][] result = math.matrixSubstraction(matrixA, matrixB);
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
});
@ -111,15 +142,34 @@ public class MatrixCalcController {
MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText();
if (util.checkInput(stringMatrixA)) {
boolean stop = false;
try {
util.checkInput(stringMatrixA);
} catch (IllegalArgumentException e) {
stop = true;
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
if (util.checkInput(stringMatrixA) && !stop) {
double[][] matrixA = util.stringToMatrix(stringMatrixA);
try {
double result = math.calcDeterminat(matrixA);
String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
});
@ -138,8 +188,6 @@ public class MatrixCalcController {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
e.printStackTrace();
}
if (util.checkInput(stringMatrixB) && !stop) {
@ -156,8 +204,6 @@ public class MatrixCalcController {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
e.printStackTrace();
}
}
});

4
src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java

@ -42,7 +42,7 @@ public class MatrixCalcIOUtils {
* @param matrix It is the InputMatrix
* @return true if the Matrix is valid Input.
*/
protected boolean checkInput(String matrix) {
protected boolean checkInput(String matrix) throws IllegalArgumentException {
boolean isMatched = false;
if (matrix.length() == 0) {
throw new IllegalArgumentException("Please insert a Matrix");
@ -61,6 +61,8 @@ public class MatrixCalcIOUtils {
Matcher m = p.matcher(matrix);
isMatched = m.matches();
// TODO change the funktion to void and just throw exceptions if something went
// worng
// System.out.println(isMatched);
return isMatched;

Loading…
Cancel
Save