java, two 2D arrays and their interactions, ArrayIndexOutOfBounds Exception -


2nd question! (followup first) i'm trying list first array (namesarray) , match each index user-defined amount of hours in hoursarray. asks how many people worked week, asks names, asks hours each of people worked (while displaying names side improved quality).

i keep getting indexoutofbounds exception whenever try enter first amount of hours worked first person, how fix take amount of hours worked 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 week?: ");     int numberofemps = scan.nextint();      int numberofhours = 0;       system.out.println("\nenter names of workers entered amount (" + numberofemps + "):");       string[] namesarray = new string[numberofemps];     for(int = 0; < numberofemps; i++)     {         namesarray[i] = scan.next();     }       system.out.println("\nenter amount of hours each person worked week: ");      float[] hoursarray = new float[numberofhours];     for(int n = 0; n < namesarray.length; n++)     {         int counter = 0; // <------ need variable?         system.out.print(namesarray[n] + ": ");         hoursarray[counter] = scan.nextfloat(); // <------ error here!         system.out.println("\n");         counter++;     } } } 

your hoursarray empty :

int numberofhours = 0; .... float[] hoursarray = new float[numberofhours]; 

so hoursarray[counter] throw exception. want array have same length other array :

float[] hoursarray = new float[namesarray.length]; 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -