From 197721b332b8d1dd203db0a1e5a22e3f33c09df0 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Mon, 15 Jul 2019 10:30:44 +0200 Subject: [PATCH] Added try catch to DetA and subtraction inOrder to Display the reason why the input is Invalid --- .../matrixcalc/MatrixCalcController.java | 74 +++++++++++++++---- .../ugsbo/matrixcalc/MatrixCalcIOUtils.java | 4 +- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index 4d812c9..dddea79 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/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); - double[][] result = math.matrixSubstraction(matrixA, matrixB); - String DisplayableString = util.outputMatrixToOutputText(result); + 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); + } + + - outputText.setText(DisplayableString); - outputText.setTextAlignment(TextAlignment.CENTER); } }); @@ -111,16 +142,35 @@ public class MatrixCalcController { MatrixCalcIOUtils util = new MatrixCalcIOUtils(); String stringMatrixA = matrixATextArea.getText(); - if (util.checkInput(stringMatrixA)) { - double[][] matrixA = util.stringToMatrix(stringMatrixA); - double result = math.calcDeterminat(matrixA); + boolean stop = false; + try { - String DisplayableString = Double.toString(result); + util.checkInput(stringMatrixA); - outputText.setText(DisplayableString); + } 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); + } + } }); DetBButton.setOnMouseClicked((event) -> { @@ -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(); } } }); diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java index ae2bbb0..ab3ab7e 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java +++ b/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;