-3

So i've been at this problem for awhile, It's a piece of code that is suppose to display the data that has already been collected and sorted from the user. First ill post the code than ill post the error.

import java.util.Scanner;
import java.util.*;
public class project3
{
   private static double[] payrate;
   private static String[] names;


   public static void SortData(double payrate[])
    {
         int first;
         int temp;
         int i;
         int j;
         for(i = payrate.length - 1; i > 0; i--)
         {
            first = 0;
            for(j = 1; j<=i;j++)
            {
               if(payrate[j]<payrate[first])
               first = j;
            }
            temp = (int)payrate[first];
            payrate[first] = payrate[i];
            payrate[i] = temp; 
         } 
    }

   public static void GetData()
   {

    Scanner input = new Scanner(System.in);
     System.out.println("How many names do you want to enter?");
                String strNum = input.nextLine();
                int num = Integer.parseInt(strNum);
                int array[] = new int[num];
                for (int i = 0  ; i < array.length ; i++ ) 
                    {
                        names = new String[num];


                        System.out.println("enter employee's name: ");

                        names[i] = input.nextLine(); 
                        //while(names[i].length < 2)
                        //{
                           //System.out.println("enter valid employee's name: ");

                        //names[i] = input.nextLine(); 
                        //} 

                    }
                for(int j = 0; j < array.length;j++)
                    {
                        payrate = new double[num];
                        System.out.println("enter employee's payrate: ");
                        payrate[j] = input.nextDouble();
                        while(payrate[j] > 100 || payrate[j] < 0)
                        {
                           System.out.println("enter valid employee's payrate: ");
                           payrate[j] = input.nextDouble();
                        }


                     }


   }
        public static void DisplayData(double payrate[], String names[])
    {
         int locationsum = 0;
         for (int l=1; l<=names.length; l++) 
         {

            locationsum = 0;

           locationsum+=payrate[l];
           }

           for(int i=0;i<names.length;i++)
           {
              System.out.print(names[i]);

           System.out.printf("%6d\n", locationsum);

           } 

    }

    public static void main(String[] args)
   {
      GetData();
      SortData(payrate);
      DisplayData(payrate,names);

   }




}

I can't figure out a way to print out all of the stuff from the arrays, and with this way it keeps giving me this. The error I'm getting is :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at project3.DisplayData(project3.java:77)
    at project3.main(project3.java:94)
kyle
  • 5
  • 1

2 Answers2

4

In your DisplayData method, you're iterating up to (and including) names.length:

for (int l=1; l<=names.length; l++) 

Array indexes are zero-based, meaning the first index is always zero, and the last index is length-1. Attempting to access the index at length will give you an ArrayOutOfBoundsException because that index does not exist for any array.

Mage Xy
  • 1,693
  • 28
  • 33
-1

Change for (int l = 1; l <= names.length; l++) to for (int l = 1; l < names.length; l++).

ifly6
  • 2,517
  • 2
  • 19
  • 33