5 Replies - 869 Views - Last Post: 15 February 2012 - 10:32 PM Rate Topic: -----

#1 dylanrb123  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 15-February 12

Issues with postfix evaluator program

Posted 15 February 2012 - 08:37 PM

Hi all, so I am writing a postfix evaluator program using a stack and am having an issue. Here is my code:

Stack:
public class IntStack {

    private int count;
    private int[] theStack;
    private boolean debugging = true;

    public int getCount(){
        return count;
    }
    
    public void initialize(int i) {
	count = 0;
	theStack = new int[i];
    }

    public void initialize() {
	initialize(100);
    }

    public void push(int i) {
	if (count == theStack.length)
	    doubleIt();
	theStack[count] = i;
	count++;
    }

    public int pop() {
	if (count == 0)
	    System.out.println("You have tried to pop from an empty stack - "+
			       "program aborting");
	count = count - 1;
	return theStack[count];
    }

    private void doubleIt() {
	if (debugging)
	    System.out.println("expanding to "+theStack.length*2);
	
	int[] n = new int[theStack.length*2];
	for (int i=0; i<theStack.length;i++)
	    n[i] = theStack[i];
	theStack = n;
    }

    public boolean empty() {
	return count == 0;
    }
    
    public void print(){
        for(int i = 0;i<theStack.length;i++){
            System.out.println(theStack[i]);
        }
    }
}



Evaluator:
import java.util.Scanner;

public class PostFixEval {

    IntStack stack = new IntStack();
    
    public static void main(String[] args) {
        Scanner Keyboard = new Scanner(System.in);
        PostFixEval evaluator = new PostFixEval();
        int result = evaluator.value(Keyboard);
        System.out.println(result);
    }

    public int value(Scanner keyboard) {
        stack.initialize(10);
        String operator;
        int answer;
        int a;
        int b;
        operator = "a";
        answer = 0;
        
        while(operator != "=") {
            while(keyboard.hasNextInt() == true) {
                stack.push(keyboard.nextInt());
                stack.print();
            }
            operator = keyboard.next();
          
            b = stack.pop();
            a = stack.pop();
            
            switch(operator) {
            case "+":
                answer = a + b;
                break;
            case "-":
                answer = a - b;
                break;
            case "/":
                answer = a / b;
                break;
            case "*":
                answer = a * b;
                break;
            default:
                System.out.println("Not a valid operator");
            }
            
            stack.push(answer);
        }
        
        return answer;
    }
}



When I run it, I get an ArrayIndexOutOfBounds exception when it goes to pop from the stack, although I had it print the stack and there is definitely stuff on there when I pass in, for example, "1 2 + =" (without quotes).
Here is the command line output:
1 2 +
1
0
0
0
0
0
0
0
0
0
1
2
0
0
0
0
0
0
0
0
You have tried to pop from an empty stack - program aborting
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at IntStack.pop(IntStack.java:33)
at PostFixEval.value(PostFixEval.java:32)
at PostFixEval.main(PostFixEval.java:10)
Any help would be fantastic!
-drb

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with postfix evaluator program

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Issues with postfix evaluator program

Posted 15 February 2012 - 09:29 PM

   public int pop() {
	if (count == 0)
	    System.out.println("You have tried to pop from an empty stack - "+
			       "program aborting");
	count = count - 1;
	return theStack[count];
    }


your count = count-1 and your return should be in an else statement. Right now it is just printing an error message then trying to pop the value. You should consider throwing an exception if count==0 as well.
Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Issues with postfix evaluator program

Posted 15 February 2012 - 09:41 PM

You enter the values 1 2 + = and you are doing this:
while(keyboard.hasNextInt() == true) 


so when it gets + the loop end. After that you have:
 operator = keyboard.next();
 b = stack.pop();
 a = stack.pop();


Then the first loop start again
while(operator != "=") {


and as operator is not "=" this sentence is true so it goes to the next:
 while(keyboard.hasNextInt() == true) {


which is false keyboard.hasNextInt() is equal to +, so the next 3 lines again
operator = keyboard.next();
 b = stack.pop();
 a = stack.pop();


but the variable count is 0 and there you got the Exception in thread "main".

by the way you cannot have a string object in the switch. This does not work:
 switch(operator) 


just int values can be there.

This post has been edited by guido-granobles: 15 February 2012 - 09:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 dylanrb123  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 15-February 12

Re: Issues with postfix evaluator program

Posted 15 February 2012 - 10:03 PM

OK, so I figured out how to fix that particular issue, but now when it should print the answer, it asks for user input again, even though I only have the
 Scanner Keyboard = new Scanner(System.in); 

line once. Any ideas? Could it have to do with the fact that I pass Keyboard in as an argument to the value() method?
-drb
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Issues with postfix evaluator program

Posted 15 February 2012 - 10:25 PM

public class PostFixEval {
 
    IntStack stack = new IntStack();
     
    public static void main(String[] args) {
        Scanner Keyboard = new Scanner(System.in);
        PostFixEval evaluator = new PostFixEval();
        int result = evaluator.value(Keyboard);
        System.out.println(result);
    }

    public int value(Scanner keyboard) {



Honestly you do not need to pass the Scanner at all. Just declare and instaniate the Scanner at Class Level, underneath the Instack object creation, and then you can use it throughout the entire class. That will allow you to remove the Scanner as a parameter.
Was This Post Helpful? 0
  • +
  • -

#6 dylanrb123  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 15-February 12

Re: Issues with postfix evaluator program

Posted 15 February 2012 - 10:32 PM

I wanted to do that but my professor said the value() method had to have a Scanner parameter... for whatever reason.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1