Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why am i getting this error?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try putting some println statements into your code, to see what the variables are at each step.
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
     
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic