Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Array question

gerardagerarda Member Posts: 2
Hi all,
Im trying to print out all the values within an array bigger than 28 (see below) but can get it right?

*************************************

class fishLengthList {



public static void main(String args[]){
{

int i = 0;



double[] fishLengthList = {10.0,15.5,18.0,29.5,45.5 };
while (fishLengthList [i] < 28){
System.out.println((fishLengthList[(int) 28.0]));
}
}

}
}

************************************************

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 28
at fishLengthList.main(fishLengthList.java:29)

Comments

  • silveredge52silveredge52 Member Posts: 198
    Hey,
    see code with comments:[code]
    class fishLengthList {

    public static void main(String args[]) {
    {
    int i = 0;
    double[] fishLengthList = {10.0, 15.5, 18.0, 29.5, 45.5};
    // for loop will examine each array element
    for (i = 0; i < fishLengthList.length; i++) {

    // this statement will look at first array element, i.e. 0, and compare value to 28
    // this comparassion will never change if i is not incremented
    // while (fishLengthList[i] < 28) {
    // this statement fails because it says, print array element number 28 - there are only
    // five elements to reference
    // System.out.println((fishLengthList[(int) 28.0]));

    // if test will print array element if criteria met
    if (fishLengthList[i] < 28) {
    System.out.println((fishLengthList[i]));
    }
    }
    }
    }
    }[/code]
    regards, se52
Sign In or Register to comment.