Added a simple exception handling for division by zero attempts.
parent
a4e41180e1
commit
505f428029
|
@ -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()) {
|
||||||
result = "" + calculate(formula);
|
try {
|
||||||
|
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;
|
||||||
|
|
Loading…
Reference in New Issue