T
Taria
I have been working on the proverbial postfix program for awhile and
found this error. I am having trouble comparing objects with a
value. I tried to put the value into an object structure but it still
doesn't work. It pops beyond where I expect then has an out of
bounds error.
This is a simpler version of my program, please don't laugh. I have
cut and pasted out the problem area. The "while" condition never
returns true when it should. hrmph. Also, our professor required we
write our own Stack class instead of using java's stack class..so I
included that as well. Any hints...is appreciated.
public class MyProg{
public static void main(String[] args) {
String infixInput = "a-(b+c*d)/e";
Expression LParenthesis = new Expression("(");
Stack operatorStack = new Stack();
operatorStack.push("-");
operatorStack.push("(");
operatorStack.push("-");
operatorStack.push(")");
operatorStack.push("*");
while (operatorStack.top() != LParenthesis){
System.out.println ("Popping top off: "+operatorStack.top());
operatorStack.pop();
}
}
}
/**Class Expression storage*/
class Expression{
private String equation = "";
public Expression()
{
}
public Expression(String newExpression)
{
this.equation = newExpression;
}
public String toString(){
String string = equation;
return string;
}
}
public class Stack {
private static final int MAX_SIZE = 10;
private int count = 0;
private Object[] array;
/*
* Constructor that defines the layout of the stack
*/
public Stack (){
// instantiation of the object "stack"
this.array = new Object[MAX_SIZE];
this.count = 0;
}
public Object top(){
return this.array[count-1];
}
public boolean isEmpty(){
if (this.count == 0){
return true;
}
return false;
}
public int size(){
return this.count;
}
public boolean push(Object newItem){
if (count == MAX_SIZE){
return false;
}
this.array[count++] = newItem;
return true;
}
public Object pop(){
if (count <=0) {
return null;
}
return this.array[--count];
}
public void popAll(){
for (int i=0;i==count;i++){
array = null;
}
count = 0;
}
}
found this error. I am having trouble comparing objects with a
value. I tried to put the value into an object structure but it still
doesn't work. It pops beyond where I expect then has an out of
bounds error.
This is a simpler version of my program, please don't laugh. I have
cut and pasted out the problem area. The "while" condition never
returns true when it should. hrmph. Also, our professor required we
write our own Stack class instead of using java's stack class..so I
included that as well. Any hints...is appreciated.
public class MyProg{
public static void main(String[] args) {
String infixInput = "a-(b+c*d)/e";
Expression LParenthesis = new Expression("(");
Stack operatorStack = new Stack();
operatorStack.push("-");
operatorStack.push("(");
operatorStack.push("-");
operatorStack.push(")");
operatorStack.push("*");
while (operatorStack.top() != LParenthesis){
System.out.println ("Popping top off: "+operatorStack.top());
operatorStack.pop();
}
}
}
/**Class Expression storage*/
class Expression{
private String equation = "";
public Expression()
{
}
public Expression(String newExpression)
{
this.equation = newExpression;
}
public String toString(){
String string = equation;
return string;
}
}
public class Stack {
private static final int MAX_SIZE = 10;
private int count = 0;
private Object[] array;
/*
* Constructor that defines the layout of the stack
*/
public Stack (){
// instantiation of the object "stack"
this.array = new Object[MAX_SIZE];
this.count = 0;
}
public Object top(){
return this.array[count-1];
}
public boolean isEmpty(){
if (this.count == 0){
return true;
}
return false;
}
public int size(){
return this.count;
}
public boolean push(Object newItem){
if (count == MAX_SIZE){
return false;
}
this.array[count++] = newItem;
return true;
}
public Object pop(){
if (count <=0) {
return null;
}
return this.array[--count];
}
public void popAll(){
for (int i=0;i==count;i++){
array = null;
}
count = 0;
}
}