fix: improve error handling and user feedback in calculation logic

This commit is contained in:
Dominik Krenn 2025-09-19 14:16:42 +02:00
parent fb05209009
commit 06f16c244f

View File

@ -83,7 +83,6 @@ public class Application extends Base {
String[] tokens = trimmed.split(" "); String[] tokens = trimmed.split(" ");
if (tokens.length > 0) { if (tokens.length > 0) {
// remove last token
tokens = Arrays.copyOf(tokens, tokens.length - 1); tokens = Arrays.copyOf(tokens, tokens.length - 1);
this.formula = String.join(" ", tokens); this.formula = String.join(" ", tokens);
} else { } else {
@ -137,16 +136,13 @@ public class Application extends Base {
} }
if ("+-*/".contains(label)) { if ("+-*/".contains(label)) {
if (label.equals("-") && (trimmed.isEmpty() || trimmed.matches(".*[\\+\\-\\*/]$"))) { if (label.equals("-") && (trimmed.isEmpty() || trimmed.matches(".*[+\\-*/]$"))) {
this.formula += "-"; this.formula += "-";
this.label.setText(formula); this.label.setText(formula);
return; return;
} }
if (trimmed.isEmpty()) return;
if (trimmed.matches(".*[\\+\\-\\*/]$")) return;
this.formula += " " + label + " "; this.formula += " " + label + " ";
} else if ("=".equals(label)) { } else if ("=".equals(label)) {
if (trimmed.isEmpty() || trimmed.matches(".*[\\+\\-\\*/]$")) return;
solve(); solve();
} else if ("CLR".equals(label)) { } else if ("CLR".equals(label)) {
this.formula = ""; this.formula = "";
@ -171,13 +167,16 @@ public class Application extends Base {
} }
try { try {
double result = eval(expr); double result = eval(expr);
if (result == (long) result) { if (Double.isInfinite(result) || Double.isNaN(result)) {
this.formula = "ENGIE: DIVIDE BY ZERO, YA DUNCE!";
lastInputWasError = true;
} else if (result == (long) result) {
this.formula = String.valueOf((long) result); this.formula = String.valueOf((long) result);
} else { } else {
this.formula = String.valueOf(result); this.formula = String.valueOf(result);
} }
} catch (Exception e) { } catch (Exception e) {
this.formula = "ERR"; this.formula = "SOLDIER: YOU BROKE IT, MAGGOT!";
lastInputWasError = true; lastInputWasError = true;
} }
} }