diff --git a/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java b/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java new file mode 100644 index 0000000..3c0e7c8 --- /dev/null +++ b/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java @@ -0,0 +1,73 @@ +import java.util.*; +public class BracketsBalancedOrNot +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + String expression = in.nextLine(); + in.close(); + if(BalancedOrNot(expression)) + { + System.out.println("BRACKET BALANCED"); + } + else + { + System.out.println("BRACKET NOT BALANCED"); + } + } + private static char reverseBracket(char ch) + { + switch(ch) + { + case ')': + return '('; + case ']': + return '['; + case '}': + return '{'; + default: + return '0'; + } + } + //this method will throw empty stack exception + private static boolean BalancedOrNot(String expression) throws EmptyStackException + { + Stack S = new Stack(); + try + { + for(int i=0;i