Why am I getting this error:

Please Enter 5 variables: 
Please enter value in slot 0:1
Please enter value in slot 1:2
Please enter value in slot 2:5
Please enter value in slot 3:4
Please enter value in slot 4:2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
	at Duplicates.NumberCheck(Duplicates.java:32)
	at Duplicates.main(Duplicates.java:48)
 = 5

I have played around with the limits, and changed around a few other things, but I can't seem to get around this error! Here's the rest of my code:

import java.util.*;

public class Duplicates 
{
	Scanner input = new Scanner( System.in );
	int i;
	int cnt = 0;
	int numbers[] = new int[5];
	
	
	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();
			cnt++;
		}
		System.out.printf(" = " + cnt);
	}
	
	public void NumberCheck()
	{
		for (int j=0; j<=cnt-1; j++)
		{
			if (numbers[j] == numbers[cnt])
			{
				System.out.println("Repeated Number");
			}
			else
			{
				System.out.println(cnt +  " = " + numbers[cnt]);
			}
		}
	}
	
	public static void main(String[] args)
	{
		Duplicates DuplicateTest = new Duplicates();
		
		DuplicateTest.EnterNumbers();
		DuplicateTest.NumberCheck();
	}
}

ArrayIndexOutOfBoundsException: 5
at Duplicates.NumberCheck(Duplicates.java:32)

The array referenced at line 32 of you code does NOT have 6 elements in it.
How does the value of the index exceed the array size when you execute line 32?
If you're not sure how the value changes, print it out each time you change it.

I'm nnot sure what you're saying. I am looking through the array of 5 ints (0-4). I am building a count of them (1-5, which is why I have cnt-1 = 0-4). I then use j in place of i, so that it can loop through and check to see if any of the values have been repeated. I'm not sure about the 6 elements that you mention though....I'm not seeing that anywhere (which I admit may be the problem)....

I changed my loop around and got rid of the error, but now I'm not able to read the last value !?!

import java.util.*;

public class Duplicates 
{
	Scanner input = new Scanner( System.in );
	int i;
	int cnt = 0;
	int numbers[] = new int[5];
	int newnumbers[] = new int[5];
	
	
	public void EnterNumbers()
	{
		
		System.out.println("Please Enter 5 variables: ");
		for (i=0; i<5; i++)
		{
			System.out.printf("Please enter value in slot " + i + ":");
			numbers[i] = input.nextInt();
			cnt++;
		}
		System.out.printf(" = " + cnt+ "\n");
	}
	
	public void NumberCheck()
	{
		for (int j=0; j<=cnt-1; j++)
		{
			if (numbers[j] == numbers[cnt-1])
			{
				System.out.println(newnumbers[j]);
			}
			else
			{
				newnumbers[j] += numbers[j];
				System.out.println(newnumbers[j] + " , ");
			}
		}
	}
	
	public static void main(String[] args)
	{
		Duplicates DuplicateTest = new Duplicates();
		
		DuplicateTest.EnterNumbers();
		DuplicateTest.NumberCheck();
	}
}

Here's my output:

Please Enter 5 variables: 
Please enter value in slot 0:1
Please enter value in slot 1:2
Please enter value in slot 2:3
Please enter value in slot 3:4
Please enter value in slot 4:5
 = 5
1 , 
2 , 
3 , 
4 , 
0

I've played around with it a bit, and I still can't get it. I've changed my loops and tried everything:

public void NumberCheck()
	{
		for (int j=0; j<cnt; j++)
		{
			if (numbers[j] == numbers[cnt-1])
			{
				System.out.println(newnumbers[j]);
			}
			else
			{
				newnumbers[j] += numbers[j];
				System.out.println(newnumbers[j] + " , ");
			}
		}
	}

and

public void NumberCheck()
	{
		for (int j=0; j<=cnt-1; j++)
		{
			if (numbers[j] == numbers[cnt])
			{
				System.out.println(newnumbers[j]);
			}
			else
			{
				newnumbers[j] += numbers[j];
				System.out.println(newnumbers[j] + " , ");
			}
		}
	}

but it still won't accept the last number.....

it still won't accept the last number

Please explain.
What does the program do?
What do you want it to do?
Looking at the code, I don't see any comments explaining what it is supposed to do.

yeah, sorry I'm not good with putting in the comments until after it's done. The purpose is to have a user input 5 variables, which will be stored in a single array. Then then program will check to see if any of the variables entered are doubles. If they are doubles, the program will print to the screen all the variables except those that are the doubled variables. For example if the user inputs 0,1,5,2,5 into the array, the program will print 0,1,5,2.

Is it possible to put the comments in the code so they can be read at the same time and place as the code? Otherwise anyone viewing the code has to scroll up and down and up and down to refer to each of them

By program comments I mean providing a description of what a statement does.
For example what does this statement do and why is it coded this way:

if (numbers[j] == numbers[cnt])

BTW you still did not explain what this means:

it still won't accept the last number

The index variable "cnt" in line 29 and 35 is 5 after the method EnterNumbers() has been called. The value 5 of the "cnt" is "OutOfBounds".

You have to rewrite the method NumberCheck() because it is not functional.

The index variable "cnt" in line 29 and 35 is 5 after the method EnterNumbers() has been called. The value 5 of the "cnt" is "OutOfBounds".

You have to rewrite the method NumberCheck() because it is not functional.

I see what you're saying about cnt being 1-5, but if I change cnt to start at -1, it still only accepts four. how do you suggest changing to NumberCheck() to fit this? I've tried everything....

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.