From a1ec4a577c92756d2becbda530966c5ba0b73c4f Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Mon, 15 Jul 2019 15:06:58 +0200 Subject: [PATCH] Added try catch around the operations of addition, transponation and multiplication. --- .../matrixcalc/MatrixCalcController.java | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index fe95879..8800989 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -43,13 +43,19 @@ public class MatrixCalcController { double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixB = util.stringToMatrix(stringMatrixB); - double[][] result = math.matrixMultiplication(matrixA, matrixB); - String DisplayableString = util.outputMatrixToOutputText(result); + try { + double[][] result = math.matrixMultiplication(matrixA, matrixB); - outputText.setText(DisplayableString); - outputText.setTextAlignment(TextAlignment.CENTER); - // System.out.println(matrixATextArea.getText()); + String DisplayableString = util.outputMatrixToOutputText(result); + + outputText.setText(DisplayableString); + outputText.setTextAlignment(TextAlignment.CENTER); + } catch (IllegalArgumentException e) { + + outputText.setText(e.getMessage()); + outputText.setTextAlignment(TextAlignment.CENTER); + } }); transposeButton.setOnMouseClicked((event) -> { @@ -59,12 +65,19 @@ public class MatrixCalcController { checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1); double[][] matrixA = util.stringToMatrix(stringMatrixA); - double[][] result = math.matrixTransponation(matrixA); - String DisplayableString = util.outputMatrixToOutputText(result); + try { + double[][] result = math.matrixTransponation(matrixA); - outputText.setText(DisplayableString); - outputText.setTextAlignment(TextAlignment.CENTER); + String DisplayableString = util.outputMatrixToOutputText(result); + + outputText.setText(DisplayableString); + outputText.setTextAlignment(TextAlignment.CENTER); + } catch (IllegalArgumentException e) { + + outputText.setText(e.getMessage()); + outputText.setTextAlignment(TextAlignment.CENTER); + } }); addButton.setOnMouseClicked((event) -> { @@ -76,12 +89,19 @@ public class MatrixCalcController { double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixB = util.stringToMatrix(stringMatrixB); - double[][] result = math.matrixAddition(matrixA, matrixB); - String DisplayableString = util.outputMatrixToOutputText(result); + try { + double[][] result = math.matrixAddition(matrixA, matrixB); - outputText.setText(DisplayableString); - outputText.setTextAlignment(TextAlignment.CENTER); + String DisplayableString = util.outputMatrixToOutputText(result); + + outputText.setText(DisplayableString); + outputText.setTextAlignment(TextAlignment.CENTER); + } catch (IllegalArgumentException e) { + + outputText.setText(e.getMessage()); + outputText.setTextAlignment(TextAlignment.CENTER); + } }); substractButton.setOnMouseClicked((event) -> { @@ -101,7 +121,7 @@ public class MatrixCalcController { outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); - } catch (Exception e) { + } catch (IllegalArgumentException e) { outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER);