My push method for my stack is not working and I do not understand why. Eclipse says the argument is not applicable for the primitive type for my Stack.
Stack<Bracket> openingBrackets = new Stack<>();
for (int position = 1; position <= text.length(); ++position) {
char next = text.charAt(position - 1);
if ( next == '(' || next == '[' || next == '{') {
openingBrackets.push(next);
}
System.out.print(openingBrackets.pop());
}
I'm expecting the (, [, { to get added to the stack
In order to initialize or declare a Stack in Java you must specify the type within the carrots
<>.Java collections are generic and these brackets are used to define the type of the data stored inside. e.g.
Stack<Integer>. In this case you're pushing characters so you have two options:Stack<Character> openingBrackets = new Stack<Character>();(Recommended)Stack<Object> openingBrackets = new Stack<Object>();Side note: only
pop()if there are items in the stack. So be sure to check for that :)