hi evry one ,im not used to java and i hate its bad compiler
can any one tell me whay it give me this running time error:array index out of bound!

this is the code were it have the erorr:

public class RotatingBieChart extends JApplet {

 int diamter,radius;
 int partition;
 final int WIDTH = 700, HEIGHT =600;
 Color colr;
  Color []colors;
 public void init() {
        setSize(WIDTH, HEIGHT);
        diamter=getDiameter();
        partition=getNumberOFslice();
     //  colr=randomColor();
}

private int getNumberOFslice()
{
       int x=0;
       String dimaeter,partitions;
       Boolean notInput2=true;

          while(notInput2){
            try{
       partitions =JOptionPane.showInputDialog(null,"enter number of partitions in the pie chart");
        x=Integer.parseInt(partitions);
        notInput2=false;
        colors =new Color[x];
        for(int i=0;i<=colors.length;i++)
        {    
            colors[i]=randomColor();        }   }
            catch(NumberFormatException nfe){
                notInput2=true;
          //do nothing just complete the loop
            }    }//end of while
        return x;
}


 private Color randomColor()
    {    Random rand;
         rand=new Random();
        colr=new Color(rand.nextInt(256),rand.nextInt(256), rand.nextInt(256));
         return(colr);
    }
}

the green is were it tell me that there is an erorr in it...

thank you in advance:)

What do we know about array? Array of 30 length does start from element position 0 (zero) and ends with element at position 29. Looking at your for loop condition which is i<=colors.length meaning loop while "i" is smaller then or EQUAL to. Tell me what happens when i=30 and we will request element from position 30?

Edited 5 Years Ago by peter_budo: n/a

ok that is why it tell me out of bound
so i should start the i from 1 !!

i did first i<=x;
and i<=colors.lenght
how will the for loop knows when to stop then?

Start from i=0, end with i<colors.length, increased i by 1 each step...
Not Start from i=1, end with i<=colors.length, increased i by 1 each step.

Edited 5 Years Ago by Taywin: n/a

In case of i<=colors.length meaning loop while "i" is smaller then or EQUAL to

  • on i=28 check condition is TRUE execute inside brackets code
  • on i=29 check condition is TRUE execute inside brackets code
  • on i=30 check condition is TRUE execute inside brackets code (and you getting error)
  • on i=31 (ignoring previous error) check condition would be FALSE stop looping

n case of i<colors.length meaning loop while "i" is smaller then

  • on i=28 check condition is TRUE execute inside brackets code
  • on i=29 check condition is TRUE execute inside brackets code
  • on i=30 check condition is FALSE stop looping

Is this clear now?

one simple question i wonder..
what can and cannot do in the init() function

You can do anything there. It's just a method like any other method you want. You can also name it differently if you want!

This article has been dead for over six months. Start a new discussion instead.