1

2nd question! (followup to my first) I'm now trying to list the first array (namesArray) and match each index with the user-defined amount of hours in hoursArray. So it asks for how many people worked for the week, then asks for their names, then asks the hours each of those people worked (while displaying their names to the side for improved quality).

I keep getting an IndexOutOfBounds exception whenever I try to enter the first amount of hours worked by the first person, how do I fix this to take the amount of hours worked by each person correctly?

import java.util.Scanner;
import java.util.Arrays;

class Tips_Calc 
{
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    System.out.print("How many employees for the week?: ");
    int numberOfEmps = scan.nextInt();

    int numberOfHours = 0;


    System.out.println("\nEnter names of workers up to the entered amount (" + numberOfEmps + "):");


    String[] namesArray = new String[numberOfEmps];
    for(int i = 0; i < numberOfEmps; i++)
    {
        namesArray[i] = scan.next();
    }


    System.out.println("\nEnter the amount of hours each person worked for the week: ");

    float[] hoursArray = new float[numberOfHours];
    for(int n = 0; n < namesArray.length; n++)
    {
        int counter = 0; // <------ do i even need this variable?
        System.out.print(namesArray[n] + ": ");
        hoursArray[counter] = scan.nextFloat(); // <------ the error is here!
        System.out.println("\n");
        counter++;
    }
}
}
Ryan Horner
  • 123
  • 1
  • 1
  • 7
  • Do you get an IndexOutofBoundsException, or an ArrayIndexOutOfBoundsException ? you should declare the counter variable outside of the for loop. – Stultuske Aug 25 '15 at 08:58

2 Answers2

4

Your hoursArray is empty :

int numberOfHours = 0;
....
float[] hoursArray = new float[numberOfHours];

So hoursArray[counter] would throw an exception. You probably want this array to have the same length as the other array :

float[] hoursArray = new float[namesArray.length];
Eran
  • 387,369
  • 54
  • 702
  • 768
0

IndexOutofBounds can be two things:

1) Either you access a field which does not exist (e.g. an array with the length of 5 and you access the index[5] (arrays start with zero!, so the last one is [4])

2) or your field has not yet been initialized.

also you can get rid of the counter variable in that place. you will always get counter==0 as you have the initialization inside of the loop. Initialize it outside of the loop and increment it inside after/before each loop.

Cribber
  • 2,513
  • 2
  • 21
  • 60
  • I just found a more elegant solution for your counter btw: just use the "n" which counts for the loop. You use it already (print out) and as you increment it after the loop (thus beginning with zero in the loop) you can just delete the "counter" and replace it with the "n" variable ==> hoursArray[counter] => hoursArray[n]. The less variables you have the more overview you get. – Cribber Aug 25 '15 at 09:25