1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Rep Power

    Java.lang.ArrayIndexOutOfBoundsException error

    I have an object and client class created which prints coordinates in order of their distance from the origin. The client class asks the user how many dimensions they want the array to have (x,y or x,y,z) how many points they want generated, and a range from which each coordinate will run from (ex. -x to x, -y to y etc.). When I run the program with out the sortArray methods, it prints the correct points. (the distance is the last element of the coordinate). However, when I add the sortArray method to the client class, it allows me to enter the information but gives me the error: ---why is this happening?

    Java Code:
    java.lang.ArrayIndexOutOfBoundsException: 5
        at arrayObjects.sortArray(arrayObjects.java:130)
        at arrayClass.main(arrayClass.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

    The error is happening at:
    Java Code:
    while (newArray[j][mydimension + 1] > pivot[mydimension]) {
     while (newArray[j][mydimension + 1] > pivot[mydimension]) {

    client class

    Java Code:
    import java.io.*;
    public class arrayClass
      public static void main (String [] args) throws IOException
        BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));
        int points = 0, dimension = 0;
        double lengthA = 0;
        double dataPoints [][] = new double [points][dimension + 1];
        System.out.println("How many dimensions do you want?");
        String dimensionA = myInput.readLine();
        dimension = Integer.parseInt(dimensionA);
        System.out.println("How many points do you want?");
        String pointsA = myInput.readLine();
        points = Integer.parseInt(pointsA);
        System.out.println("Enter a range: ");
        String lengthB = myInput.readLine();
        lengthA = Double.parseDouble(lengthB);
        arrayObjects objects;
        String myString = "arrayPoints.txt";
        objects = new arrayObjects(lengthA, points, dimension, dataPoints);
        objects.fillArray(objects.getArray(), objects.getRange(), objects.getDimension(), objects.getNumPoints());
        objects.findDistance(objects.getArray(), objects.getNumPoints(), objects.getDimension()); 
        objects.sortArray(objects.getArray(), objects.getDimension(), 0, objects.getNumPoints());
        objects.writeData(objects.getArray(), objects.getNumPoints(), myString);
      }//end main method

    object class

    Java Code:
    import java.io.*;
    import java.util.Arrays;
    public class arrayObjects
      private int dimension;
      private double lengthA;
      private int points;
      private double [][] dataPoints;
      //empty constructor
      public arrayObjects(){
      public arrayObjects(double[][] datapoints)
        lengthA = 0;
        points = 0;
        dimension = 0;
        this.dataPoints = new double [points][dimension +1];
      public arrayObjects(double myLength, int myPoints, int myDimension, double[][] dataPoints){
        lengthA = myLength;
        points = myPoints;
        dimension = myDimension;
        this.dataPoints = new double [points][dimension + 1];
      //array getter
      public double[][] getArray(){
        //findDistance(dataPoints, points, dimension);
        return dataPoints;
     //array setter
      public void setArray(double [][] myDataPoints, int dimension, int points){
        dataPoints[points][dimension] = myDataPoints[points][dimension];
      //dimension getter
      public int getDimension(){
        return dimension;
      //dimension setter
      public void setDimension(int myDimension){
        dimension = myDimension;
      //points getter
      public int getNumPoints(){
        return points;
      //points setter
      public void setNumPoints(int myNumPoints){
        points = myNumPoints;
      //range getter
      public double getRange(){
        return lengthA;
      //range setter
      public void setRange(double myRange){
        lengthA = myRange;
      //fill arrays with values
      public void fillArray(double [][] mydataPoints, double mylengthA, int mydimension, int mypoints)throws IOException
        for(int x = 0; x < mypoints; x++){//runs x times to print x data points
          for (int y = 0; y < mydimension; y++){//runs y times to print a coordinate
            mydataPoints [x][y]= (dimension *Math.random() - 1) * mylengthA;// finds a random number in the range and assigns it to the coordinate array
          }//end for
        }//end for
        }//end fillArray
      //reads data from file
      public void readData(String myString)throws IOException
        BufferedReader readfile = new BufferedReader(new FileReader(myString));//reads data from arrayPoints.txt
        try {//try statement
          StringBuilder file = new StringBuilder();
          String line = readfile.readLine();//assigns line data from file
          while (line != null) {//run while line isn't empty
            line = readfile.readLine();
          }//end while
          String finalData = file.toString();//converts to a string
          System.out.print(finalData);//prints the coordinate
        } //end try
        finally {//close file when done
        }//end finally
      }//end readFile
      //write data to a file
      public void writeData(double[][] mydataPoints, int mypoints, String myString)throws IOException
        PrintWriter fileOut = new PrintWriter (new FileWriter (myString));//writes to myString file
        for (int i = 0; i < mypoints; i++){
          fileOut.println(Arrays.toString(mydataPoints[i]));//converts array to string and prints to file
        }//end for
        fileOut.close();//writes to file
      }//end writeData
      //calculates distance between coordinates
      public void findDistance(double[][] mydataPoints, int mypoints, int mydimension){
        for(int i = 0; i < mypoints; i++){
          for (int j = 0; j < mydimension + 1; j++){
            mydataPoints[i][mydimension] = mydataPoints[i][j] * mydataPoints[i][j];//calculaes distance using distane formula
          mydataPoints[i][mydimension] = Math.sqrt(mydataPoints[i][mydimension]);
          }//end for
        }//end findDistance
      //quick sort for array
      public double[][] sortArray(double[][] newArray, int mydimension, int down, int top) {
        System.arraycopy(newArray, 0, newArray, 0, newArray.length);
        int i = down;//sets int variables
        int j = top;
        double [] pivot = newArray[(down + top) / 2];//finds centre pivot point
        //goes through each value and sorts depending if its greater or less than the pivot point
        do {
          while (newArray[i][mydimension] < pivot[mydimension]) {
          while (newArray[j][mydimension] > pivot[mydimension]) {
          if (i <= j) {
            double[] temp = new double[newArray[i].length];
            for (int y = 0; y < newArray[i].length; y++) {
              temp[y] = newArray[i][y];
              newArray[i][y] = newArray[j][y];
              newArray[j][y] = temp[y];
        } while (i <= j);
        if (down < j) {
          newArray = sortArray(newArray, mydimension, down, j);
        if (i < top) {
          newArray = sortArray(newArray, mydimension, i, top);
        //returns array
        return newArray;
      }//end sortArray
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Mobile, Alabama
    Rep Power
    Note what j is here

    public double[][] sortArray(double[][] newArray, int mydimension, int down, int top) {
        System.arraycopy(newArray, 0, newArray, 0, newArray.length);
        int i = down;//sets int variables
        int j = top;
    Remember that the parameter top seems to be the number of elements.

IMN logo majestic logo threadwatch logo seochat tools logo