<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>
Explanation / Answer
You were really close. Those two lines were giving you an error because you hadn't specified what data types the stack would be holding. By using the following line when you declare your stack: Stack stack = new Stack(); it will assume you are putting a character in and getting one out when you do a push/pop operation. Additionally, you needed to pull the if stack.isempty()/else check out of the while loop so that it only does it at the end. Nice work. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Stack; public class BalanceCheckerStack { public static void main(String[] args)throws IOException { System.out.println("Enter file name: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String file = in.readLine(); Scanner s = new Scanner(new FileReader(file)); Stack stack = new Stack(); char ch; char ch1; while(s.hasNext()) { String source = s.nextLine(); for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.