• Post Reply Bookmark Topic Watch Topic
  • New Topic

why am i getting this error?  RSS feed

 
Prashanth Vijayai
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class temp {

/**
* @param args
*/
public static void main(String[] args)
{

// TODO Auto-generated method stub
int[] clinic=new int[4];
String[] interval=new String[17];
String[] appoint=new String[16];



int k=0;

for(int i=0;i<4;i++)
{
clinic[i]=4+i;
(this place)for(int j=0;j<=60;j=j+15)
{
if(j==60)
{
interval[k++]=clinic[i]+1+".00";
}
else
{
interval[k++]=clinic[i]+"."+j;
}
}
}
for(k=0;k<16;k++)
{
appoint[k]=interval[k]+"-"+interval[k+1];
System.out.println(appoint[k]);
}

}

}


Im getting an error sayin that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
at temp.main(temp.java:29)

when I replace 'j' value by 1 it gives me a output
4.1-4.16
4.16-4.31
4.31-4.46
4.46-5.1
5.1-5.16
5.16-5.31
5.31-5.46
5.46-6.1
6.1-6.16
6.16-6.31
6.31-6.46
6.46-7.1
7.1-7.16
7.16-7.31
7.31-7.46
7.46-null

why am i getting that error?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try putting some println statements into your code, to see what the variables are at each step.
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
modify for(int j=0;j<=60;j=j+15)
to
for(int j=0;j<60;j=j+15)
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
at temp.main(temp.java:29)


Please check if some of the following hints may help you.

  • What exactly is the ArrayIndexOfBoundsException is? You can read it in the JavaDocs (if you dont have it, just search in google).
  • Why it occurs? Under what circumstances?
  • In my program the error is seems to have thrown at line number "17". What piece of code or part of statement might have caused it?
  • If i am able to identify the tentative code, can i avoid that?
  • If so, what measures i can take to avoid?



  • If you reached the final step and come up with the answer, you have resolved the issue!
     
    Mohammad Hossain
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Its not that hard.
    because when (j=0;j<=60;j+=15)
    then you get j=0,15,30,45,60
    when (j=1)
    you get j=1,16,31,46

    Look when you said j=0, then the loop will run (5 times for j)*(4 times for i) so total 20 times; however, interval can have 17 elements. so when it gets interval[18] gives you error.

    Then when you use j=1. The loop would run (4 times for j)*(4 times for i)
    so total 16 which is valid for interval, and it works.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!