So this is my first time learning arrays and i'm having an issue. I know that arrays start at the 0 address memory slot, and I can't seem to get it to count up to the number without getting an out of bounds exception. Here is my code...
import java.util.Scanner;
public class ArrayDemo
{
public static void main(String[] args)
{
//Variables
int Employees;
int UserInputEmployee;
//keyboard for input
Scanner Keyboard = new Scanner(System.in);
//Ask the user how many employees there are
System.out.println("How many employees do you have?");
Employees = Keyboard.nextInt();
int[] Hours = new int[Employees];
int[] EmployeeHours = new int[Employees];
//This for loop inputs the array data based on the number of employees the user enters
for(int Counter = 1; Counter < Employees; Counter++)
{
Hours[Counter] = Counter;
}
//This for Loop Inputs the EmployeeHours array based on what the user inputs
for(int CounterHours = 1; CounterHours < Employees; CounterHours++)
{
System.out.println("Please enter the number of hours for employee " + CounterHours);
UserInputEmployee = Keyboard.nextInt();
//add the numbers to the array
EmployeeHours[CounterHours] = UserInputEmployee;
}
//This for loop outputs the data that the user entered based on what the user enters
for(int CounterOutput = 1; CounterOutput < Employees; CounterOutput++)
{
System.out.println("Employee " + Hours[CounterOutput] + ":" + EmployeeHours[CounterOutput]);
}
}
}
Is there an easy way when working with arrays to get it to count up to equal to the number that the user enters? Putting an "<=" in the for loops instead of a "<" gives me the out of bounds exception.
cheers,

New Topic/Question
Reply



MultiQuote





|