From 7f77a469f67b79587c975db614d2098421fe4056 Mon Sep 17 00:00:00 2001 From: Jordi Ayala Date: Mon, 31 Jul 2023 09:59:05 -0600 Subject: [PATCH] Reto #28 - Java --- .../java/asjordi.java" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/java/asjordi.java" diff --git "a/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/java/asjordi.java" "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/java/asjordi.java" new file mode 100644 index 0000000000..8ccf3f2b24 --- /dev/null +++ "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/java/asjordi.java" @@ -0,0 +1,35 @@ +public class MathematicalExpression { + + public static boolean check(String expression){ + + String[] components = expression.split(" "); + + if (components.length < 3 || components.length % 2 == 0) return false; + + boolean check = true; + + for (int i = 0; i < components.length; i++) { + if (i % 2 == 0) check = isNumber(components[i]); + else check = isOperation(components[i]); + + if (!check) return false; + } + + return check; + + } + + private static boolean isNumber(String str) { + try { + Double.parseDouble(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + private static boolean isOperation(String str) { + return str.matches("[+\\-*/%]"); + } + +}