Advertisement
Sanlover

Untitled

Oct 26th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. package main;
  2.  
  3. import ser.SimpleExpressionSolver;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class Home {
  8. private static boolean isCorrect(String expression) throws Exception {
  9.  
  10. int depth = 0;
  11. for (int i = 0 ; i< expression.length(); i++){
  12. if(expression.charAt(i) == '('){
  13. depth++;
  14. } else if(expression.charAt(i) == ')') {
  15. depth--;
  16. }
  17. }
  18. if(depth != 0){
  19. return false;
  20. }
  21. return true;
  22. }
  23. public static void main(String args[])
  24. {
  25. try {
  26. Scanner scanner = new Scanner(System.in);
  27. boolean isGoodInput = false;
  28. System.out.print("Enter a string: ");
  29. String str =null;
  30. while(!isGoodInput){
  31. str = scanner.nextLine();
  32. if(isCorrect(str)){
  33. isGoodInput = true;
  34. } else {
  35. System.out.println("Your expression is not correct. Try again: ");
  36. }
  37. }
  38. SimpleExpressionSolver solver = new SimpleExpressionSolver();
  39. System.out.print(solver.calculate(str));
  40. } catch (Exception ex) {
  41. System.out.println(ex.getMessage());
  42. }
  43.  
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement