Results 1 to 2 of 2
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default 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:

    Java Code:
    java.lang.ArrayIndexOutOfBoundsException: 5
    	at arrayObjects.sortArray(
    	at arrayClass.main(
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    	at java.lang.reflect.Method.invoke(
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(
    client class
    Java Code:
    public class arrayClass
      public static void main (String [] args) throws IOException
        BufferedReader myInput = new BufferedReader (new InputStreamReader(;
        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.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
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: java.lang.ArrayIndexOutOfBoundsException error

    Arrays are indexed starting at 0. It appears you are using the size of the array as an inclusive upper bound in your sort method. This
    may be the source of the error.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Error: java.lang.ArrayIndexOutOfBoundsException
    By Lets Assume in forum New To Java
    Replies: 1
    Last Post: 07-13-2013, 02:43 AM
  2. java.lang.ArrayIndexOutOfBoundsException ERROR?
    By dharav in forum New To Java
    Replies: 5
    Last Post: 01-15-2012, 09:33 AM
  3. Replies: 4
    Last Post: 10-02-2011, 08:29 PM
  4. Replies: 1
    Last Post: 09-27-2011, 06:47 PM
  5. Replies: 1
    Last Post: 07-31-2007, 11:47 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