I'm doing an assignment that implements the following interface

public interface MyStackInterface {
    public Character pop();
    public void push (Character c);
    public boolean isEmpty();
    public void clear ();
}

The objective is to properly arrange parenthesis, brackets, and braces. I was wondering should I use a data file or try to hard code them into the program. And if I should use a text file, I'm asking about some ideas on the scanner interface that is used to do this..

Dani AI

Generated

If the assignment is just "check balanced parentheses/brackets/braces," keep the bracket pairs hard-coded in your program and read the input expressions from a file or stdin. The pairs are fixed, so putting them in code simplifies the logic and avoids parsing a separate config format. Use an external text file only if the exercise explicitly asks you to make the bracket set configurable or to run many test lines.

For reading input, either Scanner or BufferedReader will work. Scanner is convenient for tokenizing; BufferedReader/readLine is slightly faster and simpler when you want whole lines to scan character-by-character. Use try-with-resources to open/close the stream and process the file line by line; treat each line independently for the balance check.

A simple, robust approach is to scan characters and push openings onto a stack; when you see a closer, check that the stack is not empty and that the top matches the corresponding opener. Using an ArrayDeque<Character> and a small Map of closers-to-openers keeps the code compact and fast:

static boolean isBalancedLine(String line) {
    Map<Character,Character> closeToOpen = Map.of(')','(',']','[','}','{');
    Deque<Character> stack = new ArrayDeque<>();
    for (char ch : line.toCharArray()) {
        if (ch == '(' || ch == '[' || ch == '{') stack.push(ch);
        else if (closeToOpen.containsKey(ch)) {
            if (stack.isEmpty() || stack.pop() != closeToOpen.get(ch)) return false;
        }
    }
    return stack.isEmpty();
}

Test with empty lines, only-openers, only-closers, nested correct and incorrect order (e.g., "([)]"). If the assignment requires implementing the given stack interface, delegate internally to an ArrayDeque or, if forbidden, implement a simple node-based stack yourself. is correct that the interface must be implemented; ’s Scanner suggestion is fine for line-by-line input; and — prefer hard-coding pairs unless configurability is required.

Recommended Answers

All 2 Replies

What do you mean use a data file, text file and the other stuff? An interface is implemented like this:

class Test implements MyStackInterface
{
    //now it must override the methods
}

The scanner interface can be used to read lines, tokens, etc.

Suppose you want to open a file named "c:\test.txt" using the Scanner:

Scanner sc = new Scanner(new File("c:\\test.txt"));

To read all the lines from the file using the created scanner:

while(sc.hasNextLine())
{
    String scLine = sc.nextLine();

    //process line
}

What is the input file supposed to look like? What should the output of the program be?

:?: For more help,

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.