Use a stack to check .txt file for ('s, {'s, ['s, etc - Java

眉间皱痕 提交于 2019-12-11 16:45:42

问题


I am trying to write a method in java to search a text file that I imported for specific characters. The file is actually a java program that I designed and converted to a .txt file.

When an opening brace/bracket is found, I am supposed to add (push) it to a stack and then when a corresponding closing brace/bracket is found I am supposed to remove (pop) it from the stack.

The purpose is to see if I have the correct amount of ), }, ] and > to correspond with the (, {, [ and >. If they all match up the method should return true, if they don't it should return false.

Anyone know how I can write this?


回答1:


This is the sample implementation for balancing the brackets in a input text file

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

public class BalanceBrackets {
    private Stack<Character> symbolStack;

    public void balance(String inputText) {
        symbolStack = new Stack<Character>();
        for (int index = 0; index < inputText.length(); index++) {
            char currentSymbol = inputText.charAt(index);
            switch (currentSymbol) {
            case '(':
            case '[':
            case '{': 
                symbolStack.push(currentSymbol);
                break;

            case ')':
            case ']':
            case '}':
                if (!symbolStack.isEmpty()) {
                    char symbolStackTop = symbolStack.pop();
                    if ((currentSymbol == '}' && symbolStackTop != '{')
                            || (currentSymbol == ')' && symbolStackTop != '(')
                            || (currentSymbol == ']' && symbolStackTop != '[')) {
                        System.out.println("Unmatched closing bracket while parsing " + currentSymbol + " at " + index+1);
                        return;
                    }
                } else {
                    System.out.println("Extra closing bracket while parsing " + currentSymbol + " at character " + index+1);
                    return;
                }
                break;
            default:
                break;
            }
        }
        if (!symbolStack.isEmpty())
            System.out.println("Insufficient closing brackets after parsing the entire input text");
        else 
            System.out.println("Brackets are balanced");
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("D://input.txt"));
        String input = null;
        StringBuilder sb = new StringBuilder();
        while ((input = in.readLine()) != null) {
            sb.append(input);
        }
        new BalanceBrackets().balance(sb.toString());
    }
}


来源:https://stackoverflow.com/questions/8116924/use-a-stack-to-check-txt-file-for-s-s-s-etc-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!