Skip to content

Commit

Permalink
Merge pull request #4387 from ASJordi/main
Browse files Browse the repository at this point in the history
Reto #28 - Java
  • Loading branch information
kontroldev authored Aug 1, 2023
2 parents fe183dd + 7f77a46 commit 2da7f4d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/java/asjordi.java
Original file line number Diff line number Diff line change
@@ -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("[+\\-*/%]");
}

}

0 comments on commit 2da7f4d

Please sign in to comment.