fork download
  1. /*
  2.  * Java Program to Check for balanced paranthesis by using Stacks
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. public class ParenthesisMatching
  8. {
  9. public static void main(String[] args)
  10. {
  11. Scanner scan = new Scanner(System.in);
  12. /* Creating Stack */
  13. Stack<Integer> stk = new Stack<Integer>();
  14. System.out.println("Parenthesis Matching Test\n");
  15. /* Accepting expression */
  16. System.out.println("Enter expression");
  17. String exp = scan.next();
  18. int len = exp.length();
  19. System.out.println("\nMatches and Mismatches:\n");
  20. for (int i = 0; i < len; i++)
  21. {
  22. char ch = exp.charAt(i);
  23. if (ch == '(')
  24. stk.push(i);
  25. else if (ch == ')')
  26. {
  27. try
  28. {
  29. int p = stk.pop() + 1;
  30. System.out.println("')' at index "+(i+1)+" matched with ')' at index "+p);
  31. }
  32. catch(Exception e)
  33. {
  34. System.out.println("')' at index "+(i+1)+" is unmatched");
  35. }
  36. }
  37. }
  38. while (!stk.isEmpty() )
  39. System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched");
  40. }
  41. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class ParenthesisMatching is public, should be declared in a file named ParenthesisMatching.java
public class ParenthesisMatching
       ^
1 error
stdout
Standard output is empty