By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,789 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,789 IT Pros & Developers. It's quick & easy.

ArrayIndexOutOfBoundsException error

P: 56
Expand|Select|Wrap|Line Numbers
  1. if ((p.length == 3 || p.length == 4) && (p[0].equalsIgnoreCase("pick") || p[0].equalsIgnoreCase("take"))) {
  2.             //for(int tr = 0 ; tr<3 ;tr++){
  3.             if ((p[1].equalsIgnoreCase("crystal") && p[2].equalsIgnoreCase("ball") && CircusManager.CM == true)
  4.                     || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("crystal") && p[3].equalsIgnoreCase("ball") && CircusManager.CM == true)
  5.                     || (p[1].equalsIgnoreCase("lucky") && p[2].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
  6.                     || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("lucky") && p[3].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
  7.                     || (p[1].equalsIgnoreCase("silent") && p[2].equalsIgnoreCase("shoes") && Clown.CL == true)
  8.                     || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("silent") && p[3].equalsIgnoreCase("shoes")) && Clown.CL == true) {
  9.                 if (p.length == 3) {
  10.                     Witch.bag.add(p[1] + " " + p[2]);
  11.                 } else if (p.length == 4) {
  12.                     Witch.bag.add(p[2] + " " + p[3]);
  13.                 }
  14.  
  15.                 AnimalKeeper.AK = false;
  16.                 Clown.CL = false;
  17.                 CircusManager.CM = false;
  18.                 System.out.println("done :)");
  19.                 //break;
  20.             } 
  21.             else{
  22.                 System.out.append("Invalid, try again!!");
  23.                 Parsing i = new Parsing();
  24.                 Scanner hi = new Scanner(System.in);
  25.                 String Rb = hi.nextLine();
  26.                 i.parsing(Rb);
  27.             }
  28.  
  29.  
  30.             //}
  31.         }
  32.  
  33.     public static void main(String[] args) {
  34.         Parsing j = new Parsing();
  35.         j.parsing("take the crystal");
  36.     }
when i run this code , it makes the error ArrayIndexOutOfBoundsException although it is supposed to get into the else and prints "Invalid, try again!!"
any help!!
May 17 '13 #1

✓ answered by Rabbit

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.

Share this Question
Share on Google+
7 Replies


P: 56
Expand|Select|Wrap|Line Numbers
  1. if ((p.length == 3 || p.length == 4) && (p[0].equalsIgnoreCase("pick") || p[0].equalsIgnoreCase("take"))) {
  2. //for(int tr = 0 ; tr<3 ;tr++){
  3. if ((p[1].equalsIgnoreCase("crystal") && p[2].equalsIgnoreCase("ball") && CircusManager.CM == true)
  4. || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("crystal") && p[3].equalsIgnoreCase("ball") && CircusManager.CM == true)
  5. || (p[1].equalsIgnoreCase("lucky") && p[2].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
  6. || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("lucky") && p[3].equalsIgnoreCase("necklace") && AnimalKeeper.AK == true)
  7. || (p[1].equalsIgnoreCase("silent") && p[2].equalsIgnoreCase("shoes") && Clown.CL == true)
  8. || (p[1].equalsIgnoreCase("the") && p[2].equalsIgnoreCase("silent") && p[3].equalsIgnoreCase("shoes")) && Clown.CL == true) {
  9. if (p.length == 3) {
  10. Witch.bag.add(p[1] + " " + p[2]);
  11. } else if (p.length == 4) {
  12. Witch.bag.add(p[2] + " " + p[3]);
  13. }
  14.  
  15. AnimalKeeper.AK = false;
  16. Clown.CL = false;
  17. CircusManager.CM = false;
  18. System.out.println("done :)");
  19. //break;
  20. }
  21. else{
  22. System.out.append("Invalid, try again!!");
  23. Parsing i = new Parsing();
  24. Scanner hi = new Scanner(System.in);
  25. String Rb = hi.nextLine();
  26. i.parsing(Rb);
  27. }
  28.  
  29.  
  30. //}
  31. }
  32.  
  33. public static void main(String[] args) {
  34. Parsing j = new Parsing();
  35. j.parsing("take the crystal");
  36. }
May 17 '13 #2

Rabbit
Expert Mod 10K+
P: 12,422
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.
May 17 '13 #3

Oralloy
Expert 100+
P: 983
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:

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   //--stuff to try here
  4. }
  5. catch(Exception exception)
  6. {
  7.   System.err.println("\n\nProgram execution failed:");
  8.   System.err.println(exception.getMessage());
  9.   System.err.println("StackTrace: ");
  10.   exception.printStackTrace(System.err);
  11. }
May 17 '13 #4

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

P: 56
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 :)
May 18 '13 #6

Rabbit
Expert Mod 10K+
P: 12,422
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.
May 18 '13 #7

Oralloy
Expert 100+
P: 983
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
May 20 '13 #8

Post your reply

Sign in to post your reply or Sign up for a free account.