1
0
Fork 0

Added a simple exception handling for division by zero attempts.

pull/1/head
Oliver Hummel 2023-12-14 08:54:48 +01:00
parent a4e41180e1
commit 505f428029
1 changed files with 8 additions and 3 deletions

View File

@ -126,8 +126,13 @@ public class Spreadsheet {
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
result = "TODO"; // TODO result = "TODO"; // TODO
else if (!formula.isEmpty()) else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula); result = "" + calculate(formula);
} catch(ArithmeticException ae) {
result = "exc.";
}
}
cells[row][col].setValue("" + result); cells[row][col].setValue("" + result);
} }
@ -153,7 +158,7 @@ public class Spreadsheet {
* @param formula The expression to be evaluated. * @param formula The expression to be evaluated.
* @return The result calculated. * @return The result calculated.
*/ */
private long calculate(String formula) { private long calculate(String formula) throws ArithmeticException {
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
long res = 0; long res = 0;