Results 1 to 5 of 5
  1. #1
    ragnor2004 is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default Array Index problems

    Ok well i bet this is a pretty noob mistake but i'm trying to get a program to run (part of the code was given to the class by the teacher). It seems his finval function works fine but i get this error when i run the program Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1, This program should take my equation in the string and parse through it using the Arraylists as stacks to show how math is dont on the stack. For your knowledge the $ is the sign being used for modulus and the @ sign is being used for exponents ( dont ask, my teacher wants it that way), so any help would be great thanks for looking at my code.
    Here is my code and later my output
    Java Code:
    package javaapplication6;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter[/COLOR];
    import java.util.ArrayList;
    
    /**
     *
     * @author Shane
     */
    public class Main {
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException {
            PrintWriter output;
            output = new PrintWriter(new File("JavaStackParce.txt"));
            GenericManagerStacks<Opertobj> oper=new GenericManagerStacks<Opertobj>();
            System.out.println("pushing operator # with priority -100");
            // System.out.println("");
            Opertobj pnode1=new Opertobj('#',-100);
            oper.pushnode(pnode1);
            GenericManagerStacks<Integer> opnd=new GenericManagerStacks<Integer>();
            char[] vart={'A','B','C','D','E','F','0','1','2','3','4','5','6','7','8','9'};
            char[] opert={'*','/','+','-',')','(','#','@','$'};
            int[] ivalue={8,12,2,3,-15,4,0,1,2,3,4,5,6,7,8,9};
            int[] intvalp={2,2,1,1,99,-99,-100,3,2};
            int oprior,exvalue,i,ivalu;
            char[] express1={'A','@','2','*','(','A','-','C','*','D',')','+','(','9','*','B','/','(','2',
            '*','C','+','1',')','-','B','*','3',')','+','E','$','(','F','-','A','#'};
            i=0;
            while(express1[i]!='#'){
                System.out.println("parsing"+express1[i]);
                if(((express1[i]>='0')&&(express1[i]<='9'))||((express1[i]>='A')&& (express1[i]<='Z'))){
                    System.out.println("this is an operand"+express1[i]);
                    ivalu=findval(express1[i],vart,ivalue,15);
                    if(ivalu==-99){
                        System.out.println("no value in table for"+express1[i]);
                    }
                    System.out.println("were pushing it on the operand stack"+ivalu);
                    opnd.pushnode(ivalu);
                }//end of variable stack
                else{
                    System.out.println("This is an operator"+express1[i]);
                    if(express1[i]=='('){
                        System.out.println("pushing an operator on the stack"+express1[i]);
                        Opertobj pnodeo=new Opertobj(express1[i],-99);
                        oper.pushnode(pnodeo);
                    }
                    else if(express1[i]==')'){
                        while((oper.peeknode()).operator!='('){
                            popevalandpush(oper,opnd);
                        }
                        oper.popnode();
                    }
                    else{
                        oprior=findval(express1[i],opert,intvalp,7);
                        System.out.println("peeking at top of stack"+oper.peeknode().priority);
                        
                        while(oprior<=(oper.peeknode()).priority){
                            popevalandpush(oper,opnd);
                        }
                        System.out.println("pushing operator"+express1[i]+"with priority"+oprior);
                        Opertobj pnodeo=new Opertobj(express1[i],oprior);
                        oper.pushnode(pnodeo);
                    }
                }
                i++;
            }//end of while express loop
            while((oper.peeknode()).operator!='#'){
                popevalandpush(oper,opnd);
            }
            exvalue=opnd.popnode();
            System.out.println("the value for this expression is"+exvalue);
    
    
        }//end main
        public static int IntEval(int oper1, char oper, int oper2){
            int result;
            switch (oper){
                case '+': result=oper1+oper2;
                    System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                    return result;
                case '-': result=oper1-oper2;
                    System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                    return result;
                case '*': result=oper1*oper2;
                    System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                    return result;
                case '@':if(oper2>0){ result=(int) Math.pow(oper1,oper2);}
                    else{return -99;}
                    System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                    return result;
                case '$': result=oper1%oper2;
                    System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                    return result;
                case '/':
                    if(oper2!=0){result=oper1/oper2;
                        System.out.println("***eval"+oper1+oper+oper2+"*result"+result);
                        return result;}
                    else{
                        System.out.println("attempted division by zero not allowed");
                        return -99;
                    }
                default: System.out.println("bad operator"+oper);
                    return -99;
            }//end switch
        }//end IntEval
    
        public static int findval(char x,char[] vtab, int[] valtb, int last){
            int i, vreturn=-99;
            for(i=0;i<=last;i++){
                if(vtab[i]==x){
                    vreturn=valtb[i];
                }
            }
            System.out.println("Found this char"+x+"value is"+vreturn);
            return vreturn;
        }//end of findval
    
        public static void popevalandpush(GenericManagerStacks<Opertobj> x,
                GenericManagerStacks<Integer> y){
            int a,b,c;
            char operandx;
            operandx=(x.popnode()).Getopert();
            if(y.number<0){return;}
            a=y.popnode();
            b=y.popnode();
            System.out.println("in popevel"+b+operandx+a);
            c=IntEval(b,operandx,a);
            y.pushnode(c);
            return;
        }//end of javaapp6
    
    }
    
    class GenericManagerStacks<T>{
        protected ArrayList<T> mystack;
        protected int number;
        public GenericManagerStacks(){
            number=0;
            mystack=new ArrayList<T>(100);
        }//end constructor
        public int getnumber(){return number;}
        public int pushnode(T x){
            System.out.println("in pushnode"+number+"x is"+x);
            mystack.add(number,x);
            number++;
            System.out.println("leaving pushnode");
            return number;
        }//end of pushnode
        public T popnode(){
            T nodeval;
            nodeval=mystack.get(number-1);
            mystack.remove(number-1);
            number--;
            return nodeval;
        }// end of popnode
        public T peeknode(){
            T nodeval;
            nodeval=mystack.get(number-1);
            return nodeval;
        }//end of peeknode
        boolean stackempty(){
            if(number==0){
                return true;}
            else{
                return false;}
         }//end of stackempty
    }//end of GenericStacksManager
    class Opertobj{
        protected char operator;
        protected int priority;
    
        public Opertobj(char opert, int pri){
            operator=opert;
            priority=pri;
        }//end of Opertobj
        public int Getprior(){return priority;}
        public char Getopert(){return operator;}
    }//end of operator class
    and now my output

    Java Code:
    run:
    pushing operator # with priority -100
    in pushnode0x isjavaapplication6.Opertobj@addbf1
    leaving pushnode
    parsingA
    this is an operandA
    Found this charAvalue is8
    were pushing it on the operand stack8
    in pushnode0x is8
    leaving pushnode
    parsing@
    This is an operator@
    Found this char@value is3
    peeking at top of stack-100
    pushing operator@with priority3
    in pushnode1x isjavaapplication6.Opertobj@42e816
    leaving pushnode
    parsing2
    this is an operand2
    Found this char2value is2
    were pushing it on the operand stack2
    in pushnode1x is2
    leaving pushnode
    parsing*
    This is an operator*
    Found this char*value is2
    peeking at top of stack3
    in popevel8@2
    ***eval8@2*result64
    in pushnode0x is64
    leaving pushnode
    pushing operator*with priority2
    in pushnode1x isjavaapplication6.Opertobj@9304b1
    leaving pushnode
    parsing(
    This is an operator(
    pushing an operator on the stack(
    in pushnode2x isjavaapplication6.Opertobj@190d11
    leaving pushnode
    parsingA
    this is an operandA
    Found this charAvalue is8
    were pushing it on the operand stack8
    in pushnode1x is8
    leaving pushnode
    parsing-
    This is an operator-
    Found this char-value is1
    peeking at top of stack-99
    pushing operator-with priority1
    in pushnode3x isjavaapplication6.Opertobj@a90653
    leaving pushnode
    parsingC
    this is an operandC
    Found this charCvalue is2
    were pushing it on the operand stack2
    in pushnode2x is2
    leaving pushnode
    parsing*
    This is an operator*
    Found this char*value is2
    peeking at top of stack1
    pushing operator*with priority2
    in pushnode4x isjavaapplication6.Opertobj@de6ced
    leaving pushnode
    parsingD
    this is an operandD
    Found this charDvalue is3
    were pushing it on the operand stack3
    in pushnode3x is3
    leaving pushnode
    parsing)
    This is an operator)
    in popevel2*3
    ***eval2*3*result6
    in pushnode2x is6
    leaving pushnode
    in popevel8-6
    ***eval8-6*result2
    in pushnode1x is2
    leaving pushnode
    parsing+
    This is an operator+
    Found this char+value is1
    peeking at top of stack2
    in popevel64*2
    ***eval64*2*result128
    in pushnode0x is128
    leaving pushnode
    pushing operator+with priority1
    in pushnode1x isjavaapplication6.Opertobj@c17164
    leaving pushnode
    parsing(
    This is an operator(
    pushing an operator on the stack(
    in pushnode2x isjavaapplication6.Opertobj@1fb8ee3
    leaving pushnode
    parsing9
    this is an operand9
    Found this char9value is9
    were pushing it on the operand stack9
    in pushnode1x is9
    leaving pushnode
    parsing*
    This is an operator*
    Found this char*value is2
    peeking at top of stack-99
    pushing operator*with priority2
    in pushnode3x isjavaapplication6.Opertobj@61de33
    leaving pushnode
    parsingB
    this is an operandB
    Found this charBvalue is12
    were pushing it on the operand stack12
    in pushnode2x is12
    leaving pushnode
    parsing/
    This is an operator/
    Found this char/value is2
    peeking at top of stack2
    in popevel9*12
    ***eval9*12*result108
    in pushnode1x is108
    leaving pushnode
    pushing operator/with priority2
    in pushnode3x isjavaapplication6.Opertobj@14318bb
    leaving pushnode
    parsing(
    This is an operator(
    pushing an operator on the stack(
    in pushnode4x isjavaapplication6.Opertobj@ca0b6
    leaving pushnode
    parsing2
    this is an operand2
    Found this char2value is2
    were pushing it on the operand stack2
    in pushnode2x is2
    leaving pushnode
    parsing*
    This is an operator*
    Found this char*value is2
    peeking at top of stack-99
    pushing operator*with priority2
    in pushnode5x isjavaapplication6.Opertobj@10b30a7
    leaving pushnode
    parsingC
    this is an operandC
    Found this charCvalue is2
    were pushing it on the operand stack2
    in pushnode3x is2
    leaving pushnode
    parsing+
    This is an operator+
    Found this char+value is1
    peeking at top of stack2
    in popevel2*2
    ***eval2*2*result4
    in pushnode2x is4
    leaving pushnode
    pushing operator+with priority1
    in pushnode5x isjavaapplication6.Opertobj@1a758cb
    leaving pushnode
    parsing1
    this is an operand1
    Found this char1value is1
    were pushing it on the operand stack1
    in pushnode3x is1
    leaving pushnode
    parsing)
    This is an operator)
    in popevel4+1
    ***eval4+1*result5
    in pushnode2x is5
    leaving pushnode
    parsing-
    This is an operator-
    Found this char-value is1
    peeking at top of stack2
    in popevel108/5
    ***eval108/5*result21
    in pushnode1x is21
    leaving pushnode
    pushing operator-with priority1
    in pushnode3x isjavaapplication6.Opertobj@1b67f74
    leaving pushnode
    parsingB
    this is an operandB
    Found this charBvalue is12
    were pushing it on the operand stack12
    in pushnode2x is12
    leaving pushnode
    parsing*
    This is an operator*
    Found this char*value is2
    peeking at top of stack1
    pushing operator*with priority2
    in pushnode4x isjavaapplication6.Opertobj@69b332
    leaving pushnode
    parsing3
    this is an operand3
    Found this char3value is3
    were pushing it on the operand stack3
    in pushnode3x is3
    leaving pushnode
    parsing)
    This is an operator)
    in popevel12*3
    ***eval12*3*result36
    in pushnode2x is36
    leaving pushnode
    in popevel21-36
    ***eval21-36*result-15
    in pushnode1x is-15
    leaving pushnode
    parsing+
    This is an operator+
    Found this char+value is1
    peeking at top of stack1
    in popevel128+-15
    ***eval128+-15*result113
    in pushnode0x is113
    leaving pushnode
    pushing operator+with priority1
    in pushnode1x isjavaapplication6.Opertobj@173a10f
    leaving pushnode
    parsingE
    this is an operandE
    Found this charEvalue is-15
    were pushing it on the operand stack-15
    in pushnode1x is-15
    leaving pushnode
    parsing$
    This is an operator$
    Found this char$value is-99
    peeking at top of stack1
    in popevel113+-15
    ***eval113+-15*result98
    in pushnode0x is98
    leaving pushnode
    pushing operator$with priority-99
    in pushnode1x isjavaapplication6.Opertobj@530daa
    leaving pushnode
    parsing(
    This is an operator(
    pushing an operator on the stack(
    in pushnode2x isjavaapplication6.Opertobj@a62fc3
    leaving pushnode
    parsingF
    this is an operandF
    Found this charFvalue is4
    were pushing it on the operand stack4
    in pushnode1x is4
    leaving pushnode
    parsing-
    This is an operator-
    Found this char-value is1
    peeking at top of stack-99
    pushing operator-with priority1
    in pushnode3x isjavaapplication6.Opertobj@89ae9e
    leaving pushnode
    parsingA
    this is an operandA
    Found this charAvalue is8
    were pushing it on the operand stack8
    in pushnode2x is8
    leaving pushnode
    in popevel4-8
    ***eval4-8*result-4
    in pushnode1x is-4
    leaving pushnode
    in popevel98(-4
    bad operator(
    in pushnode0x is-99
    leaving pushnode
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
            at java.util.ArrayList.get(ArrayList.java:324)
            at javaapplication6.GenericManagerStacks.popnode(Main.java:159)
            at javaapplication6.Main.popevalandpush(Main.java:133)
            at javaapplication6.Main.main(Main.java:76)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    So here the thing is you get a result, but not expected one. So what you have to do first of all is re-check the logic you've followed for this.

    ArrayOutOfBoundException means you are accessed an invalid index in the array.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    i get this error when i run the program Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    The runtime error message should tell you which line caused the error. Copy and post the whole thing and indicate which line of your code it is referring to.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If your list is empty, the following will (and does) cause the exception you are seeing:

    Java Code:
        public T popnode(){
            T nodeval;
            nodeval=mystack.get(number-1);
            mystack.remove(number-1);
            number--;
            return nodeval;
        }// end of popnode
    'number' starts as 0, so 'number-1', will be -1 if something goes wrong ...which appears to be happening as indicated by...

    Java Code:
    bad operator(

  5. #5
    ragnor2004 is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    The runtime errors are above in the output at the bottom which pointed to the popnode and popevalandpush

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array problems
    By Hosticus in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 02:48 AM
  3. Replies: 5
    Last Post: 10-07-2008, 05:10 PM
  4. array problems need your help ASAP!
    By notherand in forum New To Java
    Replies: 1
    Last Post: 06-29-2008, 08:59 PM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •