1

So, I'm trying to rewrite this code. The idea is to input lines from the input file, process the line one character at a time and push the characters onto a stack. (Logic to come later.) Anyways, my push method may not be working, as my show method doesn't seem to be working, either. Any suggestions would be appreciated.

while ((fileIO.hasNextLine())) {
    scanner = new Scanner(fileIO.getNextLine());
    if (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        StackADT stack = new StackADT(line.length());
        for (int i = 0; i < line.length(); i++) {
            char x = line.charAt(i);
            stack.push(x);
        }
        fileIO.writeToOutput(stack.show(stack.toString()));
    }
}

public void writeToOutput(char output){
    printer.println(output);
}

public void push(char x) {
    if (!isEmpty()) {
        a[top] = x;
        top++;
    } else {
        return;
    }
}

public char show(String stack) {

    char[] cArray = stack.toCharArray();
    char x = '\\';

    for (int i = 0; i < cArray.length; i++) {
        x = cArray[i];
    }
    return x;
}

Input (shortened for brevity):

ABB
AAABBB
AB
ABABABA
ABAB
BBAA

Output:

f
b
4
1
b
8

Edit I changed the for loop in show() to this..

@Overrride
public char show(String stack) {
    char[] cArray = stack.toCharArray();
    char x = '\\';
    for (int i = cArray.length; i >= 0; i--) {
        x = cArray[i];
    }
    return x;
}

And the push to this...

public void push(char x) {
        a[top] = x;
        top++;
}   

As for the output, currently I'm just trying to check that the input is good. Futurely, it will need to go through another class to get through the output.

3
  • 1
    It might be helpful if you posted the expected output, too. Commented Aug 3, 2018 at 5:16
  • 1
    One thing that I can see is that your push adds an element to the back of the list, and then moves the counter ahead. But your show method starts from what is at the very bottom, not from the top (your for loop needs to start at the end and work its way back). Commented Aug 3, 2018 at 5:17
  • In addition to the above, your show method yields only 1 character at a time for an entire stack. Use a StringBuilder, start from the end of the array and append the characters as you move to the front. Then, return the string builder instance. This should give you the content of the stack at a line by line basis. Commented Aug 3, 2018 at 5:21

1 Answer 1

2

Your push method doesn't work: it only pushes things on top of the stack if the stack is not empty. Since the stack starts out empty, it will never add anything.

To your strange output: if you did not override the toString() method of the StackADT class, the result of stack.toString() will be something like StackADT@54bedef2, with almost random hex digits after that @ sign.

Since your stack.show(...) method returns only the last character of this string, your output will always be a single (random) hex digit.

Sign up to request clarification or add additional context in comments.

1 Comment

This explain why the output contains number. It toke me a while to figure out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.