Skip to content

Commit

Permalink
Merge pull request #4782 from espinoleandroo/main
Browse files Browse the repository at this point in the history
Reto #28 - java
  • Loading branch information
Roswell468 authored Aug 27, 2023
2 parents b710ab6 + baa396b commit a3b7c21
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class EspinoLeandroo {

public static void main(String[] args) {
String expression1 = "5 + 16 / 7 - 4";
String expression2 = "5 a 6";

System.out.println(validateMathExpression(expression1));
System.out.println(validateMathExpression(expression2));
}

public static boolean validateMathExpression(String expression) {
String[] tokens = expression.split("\\s+");

if (tokens.length % 2 == 0) {
return false;
}

for (int i = 0; i < tokens.length; i++) {
if (i % 2 == 0) {
if (!isNumber(tokens[i])) {
return false;
}
} else {
if (!isOperation(tokens[i])) {
return false;
}
}
}

return true;
}

private static boolean isNumber(String token) {
try {
Double.parseDouble(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}

private static boolean isOperation(String token) {
return token.matches("[+\\-*/%]");
}
}

0 comments on commit a3b7c21

Please sign in to comment.