Using a stack to check balance for multiple lines of code

I am trying to write code that checks the balance for {}, () and [] in any file (with multiple lines). If the file is not balanced, please report the location (line and character number) where the error occurred. Here is my code.

import java.io.*;
import java.util.Stack;
public class CppJavaParser {
    public static class Symbol{
        public char symbol;
        public int lineFound;
        public int colFound;

        public Symbol( char symb, int line, int charIndex){
            symbol = symb;
            lineFound = line;
            colFound = charIndex;
        }
    }

    public static Stack<Symbol> symbolStack = new Stack<Symbol>();

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("HelloWorld.java"))));
        String line;
        int lineNum = 1;
        while( (line = br.readLine()) != null){
            balanceChecker(line, lineNum);
            lineNum++;
        }
        br.close();
        if (symbolStack.isEmpty()) {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }

    public static void balanceChecker(String line, int lineNum){
        int charNum = 1;
        Symbol top;
        for(char chr : line.toCharArray()){
            switch(chr) {
            case '{':
            case '(':
            case '[':
                symbolStack.push(new Symbol(chr, lineNum, charNum));
                break;

            case ']':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else
                {
                    top = symbolStack.peek();
                    if (top.symbol != '[') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            case ')':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.peek();
                    if (top.symbol != '(') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            case '}':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.peek();
                    if ((top.symbol != '{')) {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            }
            charNum++;
        }
    }
}

      

And here is the test file I'm using:

public class HelloWorld {

public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
}//This is where unmatched parenthesis error occurs

      

My program outputs the following command to the console:

Unmatched parentheses 
Success

      

How does he print success? it shouldn't print it.

+3


source to share


2 answers


    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new DataInputStream(new FileInputStream("HelloWorld.java")))); // Reading
                                                                                // the
                                                                                // test
                                                                                // file
        String line;
        int lineNum = 1;
        while ((line = br.readLine()) != null) {
            balanceChecker(line, lineNum);
            lineNum++;
        }
        br.close();
        if (symbolStack.isEmpty()) {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }

    public static void balanceChecker(String line, int lineNum) {
        int charNum = 1;
        Symbol top;
        for (char chr : line.toCharArray()) {
            switch (chr) {
            case '{':
            case '(':
            case '[':
                symbolStack.push(new Symbol(chr, lineNum, charNum));
                break;

            case ']':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else
                {
                    top = symbolStack.pop();
                    if (top.symbol != '[') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            case ')':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.pop();

                    if (top.symbol != '(') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            case '}':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.pop();

                    if ((top.symbol != '{')) {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            }
            charNum++;
        }
    }
}

      



+2


source


check your line of code: if ((symbolStack.isEmpty ()) || (top.symbol! = '[')) {===> printing error

so you check if empty (after pop) if there was one valid stack entry on the stack corresponding to the close tag, the stack is emty and it will print fail



You have to check for empty stack before appearing and then if empty print error if not pop and check for char ....

+2


source







All Articles