473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ArrayIndexOutOfBoundsException error

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.

7 2001
Norgy
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
12,516 Expert Mod 8TB
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
985 Expert 512MB
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
Norgy
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
Norgy
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
12,516 Expert Mod 8TB
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
985 Expert 512MB
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

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

Similar topics

1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
2
by: David Stevenson | last post by:
Programs from: Sheng Liang, The Java Native Interface, Programmer's Guide and Specification, The Java Series, (c) 1999, pp. 38-39. I thought I copied the program pretty exactly, but I don't know...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
31
by: ivannavi | last post by:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String Temp = jComboBox1.getSelectedItem().toString(); ...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
1
by: al666940 | last post by:
Hello everyone, I'm coding card functions (shuffle, draw, etc) using the Vector class and functions, and I'm almost finished except for an exception that I can't seem to get rid of. Can anyone...
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
2
by: David Rothabuer | last post by:
I work with an integration engine that is java based. All my scripting is javascripting and I've run into a problem that I can't find a solution to. I am getting this error when I attempt to read...
1
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import...
1
by: JanineXD | last post by:
I was browsing the web for examples on how to partition in Java. I've got to this website that has a detailed example about Partitioning so I've tried to run it on my JCreator Pro. I've only got...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.