<p>I wrote a balance-symbol checker which checks for the following pairs of symb
ID: 3640068 • Letter: #
Question
<p>I wrote a balance-symbol checker which checks for the following pairs of symbols in the source code files of Java programs: (), [], {}. This checker must implement the following algorithm:<br /><br />1. Make an empty stack.<br />2. Read symbols until the end of the source code file.<br /> a. If the symbol is an opening symbol, push it onto the stack.<br /> b. If it is a closing symbol, do the following:<br /> i. If the stack is empty, report an error.<br /> ii. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, report an error.<br />3. At the end of the file, if the stack is not empty, report an error.</p><p><br />I am getting an error on the line that says " ch1 = stack.pop()"  Any help in determining why this error is occuring would be great.  I have a feeling that there might be another underlying problem.</p>
<p><br />here is my entire class:</p>
<p><br />public class BalanceCheckerStack {<br />    public static void main(String[] args)throws IOException {<br />        System.out.println("Enter file name: ");<br />        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));<br />        String file = in.readLine();<br />        Scanner s = new Scanner(new FileReader(file));<br />        Stack stack = new Stack();<br />        char ch;<br />        char ch1;<br />        while(s.hasNext())<br />        {<br />            String source = s.nextLine();<br />            for(int i = 0; i < source.length(); i++)<br />            {<br />                ch = source.charAt(i);<br />                if(ch == '{' || ch == '(' || ch == '[')<br />                {          <br />                    stack.push(ch);<br />                }<br />                else if(ch == '}' || ch == ')' || ch == ']')<br />                {<br />                    if(stack.isEmpty())<br />                    {<br />                        System.out.println("error, the stack is empty");<br />                    }<br />                    ch1 = stack.pop();<br />                    if(ch1 == '}' && ch != '{' || ch1 == ')' && ch != '(' || ch1 == ']' && ch != '[')<br />                    {<br />                        System.out.println("error, not the cooresponding closing symbol " + ch);<br />                    }<br />                }     <br />            }<br />            if(stack.isEmpty())<br />            {<br />                System.out.println("good job! all symbols match");<br />            }<br />            else<br />            {<br />                System.out.println("error, stack is not empty, unmatched symbol present");<br />            }<br />        }<br />    }<br />} <br />       <br /><br /></p>