-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4387 from ASJordi/main
Reto #28 - Java
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/java/asjordi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[+\\-*/%]"); | ||
} | ||
|
||
} |