I can't figure out what the error means that I keep getting on my loop. I have it set to only take 5 arguments, which is my limit, but I can't tell what it's saying:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at Duplicates.EnterNumbers(Duplicates.java:20)
	at Duplicates.main(Duplicates.java:28)

Here's my code:

import java.util.*;

public class Duplicates 
{
	Scanner input = new Scanner( System.in );
	int i;
	int numbers[] = new int[4];
	
	
	public void EnterNumbers()
	{
		System.out.println("Please Enter 5 variables: ");
		for (i=0; i<=4; i++)
		{
			System.out.printf("Please enter value in slot " + i + ":");
			numbers[i] = input.nextInt();
		}
	}
	
	public static void main(String[] args)
	{
		Duplicates DuplicateTest = new Duplicates();
		
		DuplicateTest.EnterNumbers();
	}
}

On line 7 you set the size to int[4], meaning you are using indexes 0-3. In your loop you try to access index 4, which does not exist. To fix this, change line 7 to:

int numbers[] = new int[5];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.