I'm trying to find a way to search an array to see if the values entered bu the user are already in the array. For example, if numbers[0] = 1, and the user tries to input numbers[1]=1, then I want the program to kick an error back that won't allow duplicates.
import java.util.*;
public class Duplicates
{
Scanner input = new Scanner( System.in );
int i;
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();
for (int j=0; j<=4; j++)
{
if (numbers[i] == numbers[j])
{
System.out.println("Number cannot be repeated");
}
}
}
}
public static void main(String[] args)
{
Duplicates DuplicateTest = new Duplicates();
DuplicateTest.EnterNumbers();
}
}