First, I want to say that I am Java beginner and I do not have a lot of experience. This is my 2nd program.
My objective is to write a program using stacks to see if a line inside of a file such as test.txt has matching scope symbols such as "{[]}". If if it doesn't have matching scope symbols such as "[{]]", then I want to print that the symbols does not match.
Right now, I am trying to read into a file and then push a character inside of the file onto the stack. However, I do not know the keywords to do this successfully. I created my own Stack class which is presented below. Any help is appreciated.
my test.txt looks like {{}} right now.
import java.io.* ;
public class Stackmain {
public Stackmain() {
}
public static void main(String[] args) throws IOException {
Stack s = new Stack(10); // 10 chars
char ch;
FileInputStream fstream = new FileInputStream("test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((ch = (char)System.in.read()) != '\n')
if (!s.full()) s.push(ch);
}
}
Stack Class
public class Stack {
private int maxStack;
private int emptyStack;
private int top;
private char[] items;
public Stack(int size) {
maxStack= size;
emptyStack = -1;
top = emptyStack;
items = new char[maxStack];
}
public void push(char c) {
items[++top] = c;
}
public char pop() {
return items[top--];
}
public boolean full() {
return top + 1 == maxStack;
}
public boolean empty() {
return top == emptyStack;
}
}