Question: Edit the code so that instead of the example inputs at the bottom, it prompts for a file name from your computer then checks it

Edit the code so that instead of the example inputs at the bottom, it prompts for a file name from your computer then checks it.
import java.util.Stack;
public class BalancedBrackets {
public static boolean isBalanced(String s){
Stack stack = new Stack<>();
for (char c : s.toCharArray()){
if (c =='('|| c =='{'|| c =='['){
stack.push(c);
} else {
if (stack.isEmpty()){
return false;
}
char top = stack.pop();
if ((c ==')' && top !='(')||(c =='}' && top !='{')||(c ==']' && top !='[')){
return false;
}
}
}
return stack.isEmpty();
}
public static void main(String[] args){
System.out.println(isBalanced("{[()]}"));
System.out.println(isBalanced("{[(])}"));
System.out.println(isBalanced("{{[[(())]]}}"));
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!