From 04bbd02fae3ca71a2a0da68d262b234968dea104 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Mon, 15 Jul 2019 10:10:33 +0200 Subject: [PATCH] Added try catch to DetB inOrder to Display the reason why the input is Invalid --- .../matrixcalc/MatrixCalcController.java | 42 ++++++++++++++----- .../com/ugsbo/matrixcalc/MatrixCalcMath.java | 4 +- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index e378eb1..4d812c9 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -44,7 +44,7 @@ public class MatrixCalcController { double[][] result = math.matrixMultiplication(matrixA, matrixB); String DisplayableString = util.outputMatrixToOutputText(result); - + outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); } @@ -81,7 +81,7 @@ public class MatrixCalcController { double[][] result = math.matrixAddition(matrixA, matrixB); String DisplayableString = util.outputMatrixToOutputText(result); - + outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); } @@ -100,7 +100,7 @@ public class MatrixCalcController { double[][] result = math.matrixSubstraction(matrixA, matrixB); String DisplayableString = util.outputMatrixToOutputText(result); - + outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); } @@ -117,7 +117,7 @@ public class MatrixCalcController { double result = math.calcDeterminat(matrixA); String DisplayableString = Double.toString(result); - + outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); } @@ -128,15 +128,37 @@ public class MatrixCalcController { MatrixCalcIOUtils util = new MatrixCalcIOUtils(); String stringMatrixB = matrixBTextArea.getText(); - if (util.checkInput(stringMatrixB)) { + boolean stop = false; + try { - double[][] matrixB = util.stringToMatrix(stringMatrixB); - double result = math.calcDeterminat(matrixB); + util.checkInput(stringMatrixB); - String DisplayableString = Double.toString(result); - - outputText.setText(DisplayableString); + } catch (IllegalArgumentException e) { + stop = true; + + outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); + + e.printStackTrace(); + } + if (util.checkInput(stringMatrixB) && !stop) { + + double[][] matrixB = util.stringToMatrix(stringMatrixB); + try { + double result = math.calcDeterminat(matrixB); + + String DisplayableString = Double.toString(result); + + outputText.setText(DisplayableString); + outputText.setTextAlignment(TextAlignment.CENTER); + + } catch (IllegalArgumentException e) { + + outputText.setText(e.getMessage()); + outputText.setTextAlignment(TextAlignment.CENTER); + + e.printStackTrace(); + } } }); } diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java index 1666a7c..5cd807b 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java @@ -1,7 +1,5 @@ package com.ugsbo.matrixcalc; -import java.util.ArrayList; - /** * Contains all basic matrix math calculations. */ @@ -155,7 +153,7 @@ public class MatrixCalcMath { * @param matrixA The Inputmatrix * @return The Determinant of the Matrix A */ - public double calcDeterminat(double[][] matrixA) { + public double calcDeterminat(double[][] matrixA) throws IllegalArgumentException{ // checking if a Determinant can be calculated. double result = 0.0; if (checkIfMatrixIsQuadradtic(matrixA)) {