Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
    at DataSet.<init>(DataSet.java:50)
    at DataSetClient.main(DataSetClient.java:43)

    This is the error I keep getting with my code It is arrays and constructors being used, I cant find the error to correct it. I am using Eclipse by the way below is my main and my constructor class.

    import java.util.Arrays;
    import java.util.Random;
    public class DataSetClient
    {
    public static void main(String[] args)
    {
    Random generator = new Random();

    double[] randomData = new double[ generator.nextInt(4) + 10 ];
    for (int index = 0; index < randomData.length; index++)
    randomData[index] = Math.round(generator.nextDouble() * 10000) / 100.0;

    System.out.println("============ Arrays Demo ================================");
    double[] sortedData = Arrays.copyOf(randomData, randomData.length);
    Arrays.sort( sortedData );

    System.out.println( "Random\n" + Arrays.toString(randomData) );
    System.out.println( "Sorted\n" + Arrays.toString(sortedData) );
    System.out.println();

    System.out.println("============ DataSet Test ================================");
    DataSet analyzer = new DataSet( randomData );
    System.out.println(analyzer.size() + " Data Items");
    System.out.println( Arrays.toString( analyzer.getData() ) );
    System.out.println( "Minimum: " + analyzer.minimum() +
    " at index " + analyzer.indexOfMin() );
    System.out.println( "Maximum: " + analyzer.maximum() +
    " at index " + analyzer.indexOfMax() );
    System.out.println( "Data Range: " + analyzer.range() );
    System.out.println( "Median : " + analyzer.median() );
    System.out.println( "Mean : " + analyzer.mean() );
    System.out.println( "Variance : " + analyzer.variance() );
    System.out.println( "Std Dev : " + analyzer.standardDeviation() );

    }

    }


    import java.util.Arrays;
    public class DataSet
    {
    //Instance Data
    private double[] data;
    private int indexMax = 0;
    private double maximum = 0.0;
    private int indexMin = 0;
    private double minimum = 0.0;
    private double median = 0.0;
    private double mean = 0.0;
    private double variance = 0.0;

    //Constructor: Create a new DataSet
    // Parameter : data points to be included into this DataSet
    // Exception : the number of data points must be at least 2
    public DataSet(double[] data)
    {
    if (data.length < 2)
    throw new RuntimeException("Data points must be at least 2");

    else
    this.data = data;

    //find index of max, indexof min and mean
    double sum = 0;
    for(int i = 0; i < data.length; i++)
    {
    if(data[i] > data[i+1] )
    indexMax = i;

    else if(data[i] < data[i+1] )
    indexMin = i;

    sum = sum + data[i];
    }
    mean = sum / data.length;


    // find median
    double[] sortedData = Arrays.copyOf(data, data.length);
    Arrays.sort( sortedData );

    int medianPosition;
    medianPosition = data.length / 2;

    median = data[medianPosition];



    //Find variance
    double difference= 0.0;
    for(int in = 0; in < data.length; in++)
    {
    difference = difference + (data[in] - mean) * (data[in] - mean);

    }
    variance = difference / data.length;





    }

    //Return the number of data-points in this DataSet
    public int size()
    {
    return data.length;
    }

    //Accessor : Return copies of the data-points in this DataSet
    public double[] getData()
    {
    return data;
    }

    // position of the biggest value
    //Return the index of the largest point in this DataSet
    public int indexOfMax()
    {

    return indexMax;
    }

    //same as indexMas
    //Return the value of the largest point in this DataSet
    public double maximum()
    {

    return maximum = data[indexMax];
    }

    //Return the index of the smallest point in this DataSet
    public int indexOfMin()
    {
    return indexMin;
    }

    //Return the value of the smallest point in this DataSet
    public double minimum()
    {
    return minimum = data[indexMin];
    }

    //Return the difference between the largest and smallest
    // points in this DataSet
    public double range()
    {
    return maximum - minimum;
    }

    //Return the value of the median point in this DataSet
    public double median()
    {
    return median;
    }

    //Return the average value of the points in this DataSet
    public double mean()
    {
    return mean;
    }

    //difference bettween the item and the mean add them and then divide
    //Return the variance of the points in this DataSet
    public double variance()
    {
    return variance;
    }

    //Return the standard deviation of the points in this DataSet
    public double standardDeviation()
    {
    return variance / variance;
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

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

    Please read the Announcement topic to learn how to post code in code or highlight tags and other useful info for newcomers. Please fix your post, if you're allowed. Another thing you should do is post the exact error, copied from as it appears at your end and pasted into a post.

    Welcome!

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Thank you I didn't know, I am new to this site and this is my first posting

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

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

    Quote Originally Posted by beer1214 View Post
    	      for(int i = 0; i < data.length; i++)
    	      {
    	           if(data[i] > data[i+1] )
    	            indexMax = i;  
     
    	           else if(data[i] < data[i+1] )
    	            indexMin = i;
    the i+1 goes out of bounds (when i reaches the last valid index).
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Thank you, I deleted the +1 and left it as [i] and the program compiles properly now, but is that correct?

Similar Threads

  1. Exception in thread "main" java.lang.NoSuchMethodError: main?
    By Sakharam01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 08:06 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM