Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Urgent help

  • 06-11-2012 12:44am
    #1
    Registered Users, Registered Users 2 Posts: 255 ✭✭


    Test 2mro, kinda need help. It says

    "java.lang.ArrayIndexOutOfBoundsException:10"

    I cant see where is going wrong.



    import java.lang.Math;
    import javax.swing.JOptionPane;
    public class WK912

    {
    public static void main(String [] args)

    {
    int numberofnumbers, odd, aNumber, sum= 0, even= 0, neven = 0, max = 0, min = 0;
    String ofnumbers= " ";
    double avg = 0.0;
    int [] numbers = new int [10];

    for(int counter = 0; counter < numbers.length; counter ++)
    {
    aNumber = (int) ((Math.random () *70)+20);
    numbers[counter] = aNumber;
    sum += aNumber;
    ofnumbers += aNumber + ", ";
    }
    avg = (sum/numbers.length);
    for(int counter = 0 ; counter < numbers.length; counter++)
    {
    if(numbers[counter] % 2 ==0)
    {
    even +=1;

    }
    else
    {
    neven +=1;
    }

    }
    int counter1 = 0;
    max = numbers[0];
    for( counter1 = 0; counter1 < numbers.length; counter1 ++);
    {
    if(numbers[counter1] > max)
    {
    max = numbers[counter1];
    }

    }

    min = numbers[0];
    for( counter1 =0; counter1 < numbers.length; counter1++)
    {
    if(numbers[counter1] < min)
    {
    min = numbers[counter1];
    }
    }

    JOptionPane.showMessageDialog(null, ofnumbers + "\n The average is " + avg + "\n The sum is " + sum + "\n There are " + even + "numbers" + "\n There are " + neven + " numbers" + max + " " + min);
    }
    }


Comments

  • Moderators, Technology & Internet Moderators Posts: 11,017 Mod ✭✭✭✭yoyo




  • Registered Users, Registered Users 2 Posts: 255 ✭✭SellingJuan


    I have it working now, all I did was change counter to =1



    But that shouldnt have been the problem like :)


  • Registered Users, Registered Users 2 Posts: 23,084 ✭✭✭✭Esel
    Not Your Ornery Onager


    Is this line 39? (presumably blank lines are counted?)

    if(numbers[counter1] > max)

    I actually don't know Java, just trying to help you as you've got a deadline.....

    Not your ornery onager



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 255 ✭✭smokiebeverage


    check the ; on line 37

    for( counter1 = 0; counter1 < numbers.length; counter1 ++);


  • Registered Users, Registered Users 2 Posts: 23,084 ✭✭✭✭Esel
    Not Your Ornery Onager


    check the ; on line 37

    for( counter1 = 0; counter1 < numbers.length; counter1 ++);
    Interesting that line 22 (similar code) has

    for(int counter = 0 ; counter < numbers.length; counter++)

    Is the absence of int the problem?

    Not your ornery onager



  • Registered Users, Registered Users 2 Posts: 3,889 ✭✭✭cgarvey


    check the ; on line 37

    Yup, that's the problem. The effect is that the for loop has run 10 times before it ever gets to the next line (38), by which time counter1 is now 10.


  • Registered Users, Registered Users 2 Posts: 255 ✭✭smokiebeverage


    cgarvey wrote: »
    Yup, that's the problem. The effect is that the for loop has run 10 times before it ever gets to the next line (38), by which time counter1 is now 10.

    Thanks for answering that, nature called and I had to nip off!


  • Registered Users, Registered Users 2 Posts: 23,084 ✭✭✭✭Esel
    Not Your Ornery Onager


    If counter and counter1 are array indices (and so should start at zero), should you not be testing them against (number.length-1)?

    Not your ornery onager



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 23,084 ✭✭✭✭Esel
    Not Your Ornery Onager


    Educate me here lads - were my suggestions/comments wide of the mark?

    Not your ornery onager



  • Administrators Posts: 54,424 Admin ✭✭✭✭✭awec


    Esel wrote: »
    Educate me here lads - were my suggestions/comments wide of the mark?
    Yup. Basically the error here is that the OP tried to access an index in the array that doesn't exist. It's a very generic error, you'll get really good at spotting where you made the mistake over time.

    The foreach statement is much better in situations like this. I'm not sure how foreach works in Java, but it's bound to have it.

    The counter1 variable had already been declared 2 lines above the for statement so there was no need to redeclare it within the for statement. There's no real need for the int counter1 = 0; line, putting for(int counter1 =0; ...... would have done the same thing.

    For the second one:

    for(int counter1 = 0; counter1 < numbers.length; counter++)

    If numbers has 10 items, that means it has indexes of 0-9. We are using the less than operator, not the less than or equals, so there's no need to do .length -1, seeing as in this case 9 will be the last time that the statement is true (as 10 is not less than 10).


  • Registered Users, Registered Users 2 Posts: 23,084 ✭✭✭✭Esel
    Not Your Ornery Onager


    Thanks for that.

    A few of more questions to improve my understanding, if you don't mind.

    Was the OP wrong though to 'fix' the error be "I have it working now, all I did was change counter to =1" (post 5 above)?

    Why didn't line 22 give a similar out-of-bounds error though:

    Line 22 for(int counter = 0 ; counter < numbers.length; counter++)

    Line 37 for( counter1 = 0; counter1 < numbers.length; counter1 ++);

    What is the issue with the ; in Line 37?

    Not your ornery onager



  • Registered Users, Registered Users 2 Posts: 255 ✭✭smokiebeverage


    Esel wrote: »
    Was the OP wrong though to 'fix' the error be "I have it working now, all I did was change counter to =1" (post 5 above)?

    Why didn't line 22 give a similar out-of-bounds error though:

    Line 22 for(int counter = 0 ; counter < numbers.length; counter++)

    Line 37 for( counter1 = 0; counter1 < numbers.length; counter1 ++);

    What is the issue with the ; in Line 37?

    Yes all he did was stop the error but his logic was still wrong

    the ; at the end of the line tells java the statement is complete so all it did was count to 10 so when it went to the next line the array index was 10 but the array index only goes from 0 - 9 so 10 is out of bounds. All the OP needed to do was remove the ; and the logic would be correct as the it would have moved on to the next line and looped through the indices 0 - 9 and then stopped.


Advertisement