简体   繁体   中英

private classes in method - java

I'm trying to write a program that takes an input of algebraic expressions (ie (6*3)+(7-2)) in what is apparently polish form (ie 6,3,'*',7,2,'-','+'), by putting integers on the stack and calling operands on the top two elements, then finally returning whatever's on top of the stack (hopefully there's only one element - the answer - on the stack).

I'm very new to java, so I'm sure that this is a thoroughly mundane question, but I can't seem to make my stack I'm using (named 'store') accessable and a editable by other methods in the class.

My implementation of stack seems to work fine, I'll post it if it comes 'under fire'.

My problem arises when the function has to call one of the operand subfunctions, like add(). It seems to totally forget that store exists and has been edited, and instead treats it as a null stack. I tried throwing 'this' in front of everything except the first two instances, but that didn't do too much. How can I get the function to share 'store' throughout all of its functions?

Sample run:

Eval myEval = new Eval;
myEval.evaluate(6, 3, '+', 4, 9, '*', '-'); 

Exception in thread "main" java.lang.NullPointerException
at Eval.add(Eval.java:45)
at Eval.evaluate(Eval.java:13)

public class Eval {
     public stack store;
     public Eval(){
        stack store = new stack();
    }

    public int evaluate(Object ... parts){
        for(Object part: parts){
            if(part instanceof Character){
                char operator = (Character) part;
                if(operator=='+'){
                    add();
                }
                else if(operator=='-'){
                    subtract();
                }
                else if(operator=='*'){
                    multiply();
                }
                else if(operator=='/'){
                    divide();
                }
                else{
                    throw new IllegalArgumentException("Illegal operand");
                }
            }
            else if(part instanceof Integer){
                int number = (Integer)part;
                store.push(part);
                System.out.println(store.peek());
            }
            else{
                throw new IllegalArgumentException("Expected char or int");
            }
        }
        if(store.peek()==null){
            throw new IndexOutOfBoundsException("No value to return");
        }
        return (int)store.pop();
    }
    private void add(){ 
        int x=(int)store.pop(); //error occurs on this line
        x+=(int)store.pop(); 
        store.push(x);
    }
    private void subtract(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        x=y-x;
        store.push(x);
    }
    private void multiply(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        x=y*x;
        store.push(x);
    }
    private void  divide(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        int z = Math.floorDiv(x,y);
        store.push(z);
    }
}

It looks like your constructor, Eval(), assigns a new stack to a temporary variable, named "store", which isn't the Eval class's public "store" variable. Consequently, the instance variable, "store", is null when Add is entered. Change the implementation of Eval() to:

public Eval(){
    this.store = new stack();
}

Or else, create a getter method for store (getStore) and instantiate it, if null, in that method, though, if you did that, you'd have to remember to reference it via getStore(), instead of directly.

Very likely you are trying to pop when the stack is empty. Also, it is unnecessary to cast the assignment for x when you already know the stack will contain an int. int x=(int)store.pop(); can just be int x = store.pop();

I would recommend adding a check to your operator functions such as:

if(stack.getLength() > 0) {
    //then add operation
} else {
  //throw an error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM