ArrayIndexOutOfBoundsException error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Norgy
    New Member
    • May 2013
    • 56

    ArrayIndexOutOfBoundsException error

    Code:
    if ((p.length == 3 || p.length == 4) && (p[0].equalsIgnoreCase("pick") || p[0].equalsIgnoreCase("take"))) {
                //for(int tr = 0 ; tr<3 ;tr++){
                if ((p[1].equalsIgnoreCase("crystal") && p[2].equalsIgnoreCase("ball") && CircusManager.CM == true)
                        || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("crystal") && p[3].equalsIgnoreCase("ball") && CircusManager.CM == true)
                        || (p[1].equalsIgnoreCase("lucky") && p[2].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
                        || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("lucky") && p[3].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
                        || (p[1].equalsIgnoreCase("silent") && p[2].equalsIgnoreCase("shoes") && Clown.CL == true)
                        || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("silent") && p[3].equalsIgnoreCase("shoes")) && Clown.CL == true) {
                    if (p.length == 3) {
                        Witch.bag.add(p[1] + " " + p[2]);
                    } else if (p.length == 4) {
                        Witch.bag.add(p[2] + " " + p[3]);
                    }
                     
                    AnimalKeeper.AK = false;
                    Clown.CL = false;
                    CircusManager.CM = false;
                    System.out.println("done :)");
                    //break;
                } 
                else{
                    System.out.append("Invalid, try again!!");
                    Parsing i = new Parsing();
                    Scanner hi = new Scanner(System.in);
                    String Rb = hi.nextLine();
                    i.parsing(Rb);
                }
    
    
                //}
            }
    
        public static void main(String[] args) {
            Parsing j = new Parsing();
            j.parsing("take the crystal");
        }
    when i run this code , it makes the error ArrayIndexOutOf BoundsException although it is supposed to get into the else and prints "Invalid, try again!!"
    any help!!
    Last edited by Rabbit; May 17 '13, 04:12 PM. Reason: Please use code tags when posting code.
  • Norgy
    New Member
    • May 2013
    • 56

    #2
    Code:
    if ((p.length == 3 || p.length == 4) && (p[0].equalsIgnoreCase("pick") || p[0].equalsIgnoreCase("take"))) {
    //for(int tr = 0 ; tr<3 ;tr++){
    if ((p[1].equalsIgnoreCase("crystal") && p[2].equalsIgnoreCase("ball") && CircusManager.CM == true)
    || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("crystal") && p[3].equalsIgnoreCase("ball") && CircusManager.CM == true)
    || (p[1].equalsIgnoreCase("lucky") && p[2].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
    || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("lucky") && p[3].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
    || (p[1].equalsIgnoreCase("silent") && p[2].equalsIgnoreCase("shoes") && Clown.CL == true)
    || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("silent") && p[3].equalsIgnoreCase("shoes")) && Clown.CL == true) {
    if (p.length == 3) {
    Witch.bag.add(p[1] + " " + p[2]);
    } else if (p.length == 4) {
    Witch.bag.add(p[2] + " " + p[3]);
    }
    
    AnimalKeeper.AK = false;
    Clown.CL = false;
    CircusManager.CM = false;
    System.out.println("done :)");
    //break;
    }
    else{
    System.out.append("Invalid, try again!!");
    Parsing i = new Parsing();
    Scanner hi = new Scanner(System.in);
    String Rb = hi.nextLine();
    i.parsing(Rb);
    }
    
    
    //}
    }
    
    public static void main(String[] args) {
    Parsing j = new Parsing();
    j.parsing("take the crystal");
    }

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      What the difference between the first set of code and the second set?

      p[3] is out of bounds if p.length is equal to 3.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Do you know how to print out the stack trace and determine in your code where the exception occurred?

        You can wrap your code in a try/catch block like this:

        Code:
        try
        {
          //--stuff to try here
        }
        catch(Exception exception)
        {
          System.err.println("\n\nProgram execution failed:");
          System.err.println(exception.getMessage());
          System.err.println("StackTrace: ");
          exception.printStackTrace(System.err);
        }

        Comment

        • Norgy
          New Member
          • May 2013
          • 56

          #5
          but when i write j.parsing("take the ball"); in the main, it works correctly
          what is the difference :D

          Comment

          • Norgy
            New Member
            • May 2013
            • 56

            #6
            anyway, i solved the problem by putting (p.length == 3) in a condition and putting (p.length == 4) in another if condition, thanks for both of you for replying :)

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              I'm glad you found your answer.

              The reason I asked what the difference was is that you posted two similar looking pieces of code and didn't tell us why you did that. I wasn't going to read each set of code line by line to find the difference if there even was one. It could have just been an accidental double post.

              Yes, that is indeed what was wrong with your code. I alluded to it in the second sentence of my post.

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Norgy,

                Since Rabbit pointed you to the correct solution, you should probably mark his answer as correct, so that other folks who find this post will be able to see the path that you took to your solution.

                Cheers,
                Oralloy

                Comment

                Working...