off by one error in array

first time learning arrays

Page 1 of 1

4 Replies - 5518 Views - Last Post: 27 October 2008 - 08:01 PM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

off by one error in array

Post icon  Posted 27 October 2008 - 06:24 PM

Hello everyone,
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,

Is This A Good Question/Topic? 0
  • +

Replies To: off by one error in array

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: off by one error in array

Posted 27 October 2008 - 07:10 PM

Initialize the loop counter varaibles to 0 instead of 1, then when you loop to < number the user enters you will get to all of the elements in the array.
Was This Post Helpful? 0
  • +
  • -

#3 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: off by one error in array

Posted 27 October 2008 - 07:32 PM

View PostBigAnt, on 27 Oct, 2008 - 07:10 PM, said:

Initialize the loop counter varaibles to 0 instead of 1, then when you loop to < number the user enters you will get to all of the elements in the array.


Thanks BigAnt for the reply. That will infact get the loops to behave how I would like. However I'm still getting an issue. When I want to display what day the user enters it at. It starts at 0-4 not from 1-5. Any way to accomplish this?
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: off by one error in array

Posted 27 October 2008 - 07:49 PM

In this line:
System.out.println("Please enter the number of hours for employee " + CounterHours);


The counter of the loop starts at zero but if you want it to display starting at one then you would just add 1:
System.out.println("Please enter the number of hours for employee " + (CounterHours + 1));


Was This Post Helpful? 0
  • +
  • -

#5 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: off by one error in array

Posted 27 October 2008 - 08:01 PM

View PostBigAnt, on 27 Oct, 2008 - 07:49 PM, said:

In this line:
System.out.println("Please enter the number of hours for employee " + CounterHours);


The counter of the loop starts at zero but if you want it to display starting at one then you would just add 1:
System.out.println("Please enter the number of hours for employee " + (CounterHours + 1));




Right on thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1