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.
showmethod starts from what is at the very bottom, not from the top (yourforloop needs to start at the end and work its way back).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.