Results 1 to 2 of 2
  1. #1
    vishwa_java is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    Below is simple program but i get output as below ... I dont understand why do that exception error :(

    public class Average {

    public static void main(String args[]) {

    double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
    double result = 0;
    int i;
    for(i=0; i<5; i++)

    System.out.println("i Value is" +i);

    result = result + nums[i];

    System.out.println("Average is " + result / 5);

    }

    }


    OUTPUT:
    i Value is0
    i Value is1
    i Value is2
    i Value is3
    i Value is4
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Average.main(Average.java:13)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    Loops and other program flow control structures should use braces { }. Your 'for' loop has only one line in it.

    Please go through the links I posted in your other thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 05:23 PM
  2. Replies: 4
    Last Post: 04-23-2012, 11:13 PM
  3. Replies: 4
    Last Post: 10-13-2010, 10:19 AM
  4. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •