mercredi 6 avril 2016

Why is my if statement returning false on these two integer values?

i am implementing a dumpStack() method for a compiler, to print the current runtime stack state. i have checked these two values with print statements and they are the same value yet the if statement is returning an invalid if answer. my desire out put is something like this [0,1,0][0] where there is a frame at index 3

notes framePointer is a stack that holds the index of where the next frame begins

//declared in class
    private Vector<Integer> runStack;
    private Stack<Integer> framePointer;


else {
                int nextFrame = 1;        
                int frameEnd = framePointer.get(nextFrame);
                boolean hasMore = true;

                System.out.print("[");

                //loop through stack , frameEnd is where the next frame ends
                for(int currentItem = 0; currentItem < frameEnd; currentItem++){

                    //if currentItem is one before the next frameEnd..
                    if((currentItem == (frameEnd-1)) && (hasMore == true))
                    {
                        //..print the item and then print frame delimiter
                        System.out.print(runStack.get(currentItem));
                        System.out.print("] [");

                        if (nextFrame == framePointer.lastElement()) {
                            //get the next frame end
                            System.out.println("blah blah blah");
                            frameEnd = runStack.size()-1;
                            hasMore = false;

                        } else {
                            nextFrame++;

    /***************************THIS******************************/
control gets here even though the two values are 2 with my test case
also does not execute the print statement "blah blah blah"

                            System.out.println("LAST ELEMENT: " + framePointer.lastElement());
                            System.out.println("NEXT ELEMENT: " + nextFrame);
                            frameEnd = framePointer.get(nextFrame);
                        }
                   } 

                    else {
                        System.out.print(runStack.get(currentItem));
                        if (currentItem != (runStack.size()- 1)) System.out.print(",");
                    }
                }
                System.out.print("]");
}


my netbeans stacktrace at vm.RunTimeStack.dumpStack(RunTimeStack.java:131) is the statement frameEnd = framePointer.get(nextFrame); (right after i print out the two values)

LIT 1
[1]
LIT 2
[1,2]
LIT 3
[1,2,3]
LIT 4
[1,2,3,4]
ARGS 2
[1,2] [LAST ELEMENT: 2
NEXT ELEMENT: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
    at java.util.Vector.get(Vector.java:744)
    at vm.RunTimeStack.dumpStack(RunTimeStack.java:131)
    at vm.VirtualMachine.executeProgram(VirtualMachine.java:33)
    at interpreter.Interpreter.run(Interpreter.java:39)
    at interpreter.Interpreter.main(Interpreter.java:47)
Java Result: 1

Aucun commentaire:

Enregistrer un commentaire