Hello,

I have to write a program which reads in a text file and sees the parsing symbols brackets, parentheses, and braces, and use stack implementation to have a balance symbol checker. My code compiles fine, but the output is strange, I know it has to do something with my for loop. Here is my code.

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Stack;



public class Parentheses 
{
    public static void main(String[] args) throws IOException
    {

        BufferedReader in = new BufferedReader(new FileReader("C:/Users/Desktop/SourceCode.txt"));
        String input = null;
        StringBuilder sb = new StringBuilder();
        while ((input = in.readLine()) != null)
        {
            sb.append(input);
        }   

        new Parentheses().StackBalance(sb.toString());   

    }

    public static void StackBalance(String s) 
    {
        char l_paren = '(';
        char r_paren = ')';
        char l_brace = '[';
        char r_brace = ']';
        char l_brack = '{';
        char r_brack = '}';
        char top_parse;

        Stack<Character> parsestack = new Stack<Character>();

        for(int i = 0; i < s.length(); i++)
        {
            char curr_parse = s.charAt(i);

            if (s.charAt(i) == l_paren)
            {
                parsestack.push(l_paren);
            }

            if (s.charAt(i) == l_brace)
            {
                parsestack.push(l_brace);
            }

            if (s.charAt(i) == l_brack)
            {
                parsestack.push(l_brack);
            }

            if (!parsestack.isEmpty())
            {
                top_parse = parsestack.pop();

                if(( curr_parse == '}' && top_parse != '{')

                    || (curr_parse == ')' && top_parse != '(')

                    || (curr_parse == ']' && top_parse != '['))
                {

                    System.out.println("Parsing does not match" + curr_parse + " at " + i+1);

                }
            }

                else
                {
                    System.out.println("Extra Bracket " + curr_parse + " at " + i+1);

                }

        }

                if (!parsestack.isEmpty())
                {
                    System.out.println("Not enough symbols from source");
                }

                else
                {
                    System.out.println("Symbols are formatted");
                }






    }


}

and here is my output

Extra Bracket i at 01
Extra Bracket m at 11
Extra Bracket p at 21
Extra Bracket o at 31
Extra Bracket r at 41
Extra Bracket t at 51
Extra Bracket   at 61
Extra Bracket j at 71
Extra Bracket a at 81
Extra Bracket v at 91

<over 2000 similar lines snipped JC>

Extra Bracket   at 21661
Extra Bracket } at 21671
Extra Bracket } at 21681
Symbols are formatted

I know it has to do with the for loop, but it's recognizing every character and printing everything out. I'm trying to to tweak it, but to no avail, nothing. The text file just includes the the source code I used for my actual program, the one I'm trying to fix. The txt file cannot be attached daniweb won't let me, but again, it's just the source code of my program.

what do you mean by parsing the pair of braces? do you want to print the individual blocks ?

Nice output. not bad for a bebinner to do.

commented: Do ensure that all posts contain relevant content and substance -3

Line 67: Isn't this case where the brackets DO match? (wrong message text)

Anyway, your bug is because the else on line 72 doesn't match the if you think it does. Fix your indentation and this should become clear, as will the fix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.