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();
	}
}

First off if the number has already been placed in the array, you want the user to pick a different number. Right now let's say my set is [4,0,0,0,0] after the first time thru and then I want to put in another 4 for the 2nd slot, the computer will set the 2nd slot to 4, then see that there is a duplicate so it will be like "Number cannot be repeated" and then ask the user to enter a number for the 3rd slot. In other words, you will still have a set [4,4,0,0,0] and even tho u said it can't be repeated it clearly has been. To make them re-select a number for that slot, make sure to include i--; right after that print statement.

The second issue is that you are searching for duplicates through the whole array, including the value u just entered. So basically let's take the same initial set [4,0,0,0,0] and u enter a 3 for the 2nd value (there is no duplicate so that should be fine). However, in your for-loop that checks for duplicates, it will see that there is one, because numbers[1] == numbers[1]. You don't want this; instead, the duplicate check should run from the first index up until (but NOT including) the index the user just entered.

See if you can figure it out based on these two tips. Good luck!

what do you suggest using for your second suggestion.....binary search, numbers.length,...?

In addition to the way ,as kvass points out, to solve this problem one may write a method to do the checking job:

/* The method duplicate checks the partial part (the index from 0 up to the n-1) of the array a to see if there is an b in it or not. */	
	boolean duplicate(int a[], int b, int n){  
		for (int i=0; i<n; i++) //do the for loop check on the array a from the index of 0 up to the index of n-1
		if (b== a[i]) // if there is a b in the array a
		return true;  // both the for loop and also the method call are terminated with returning value of  true
		return false; // if there is no b in the partial array a then return false.
	}

so that the for loop body includes the following operation:

while(duplicate(numbers, inputValue, i)){ /* check if the newly typed in inputValue is already in the partial array a. 
	inputValue is declared as int, i indicates the current number of values stored in the array a. */
	System.out.println("Number cannot be repeated, input in slot " +  i  + " again :"); /* i is the control variable of the for loop. since the method duplicate(...) shows the client typed in a duplicated value, one has to ask the client to redo the input  */
	inputValue = input.nextInt(); // receive the inputValued the client re-typed in 
	}

if the while loop has been passed, one may add the unrepreated value into the array:

numbers[i]=inputValue;

Edited 6 Years Ago by tong1: n/a

instead of j<=4, use j<=i

Using your first suggestion from above, and setting j<=i just gives me:

Please Enter 5 variables: 
Please enter value in slot 0:1
Number cannot be repeated
Please enter value in slot 0:1
Number cannot be repeated
Please enter value in slot 0:0
Number cannot be repeated
Please enter value in slot 0:1
Number cannot be repeated
Please enter value in slot 0:
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<=i; j++)
			{
				if (numbers[i] == numbers[j])
				{
					System.out.println("Number cannot be repeated");
					i--;
				}
			}
		}
	}
	
	public static void main(String[] args)
	{
		Duplicates DuplicateTest = new Duplicates();
		
		DuplicateTest.EnterNumbers();
	}
}

I know it would be more code consuming, but would it be easier to ask the user to input five variables for the array (i.e. array[0], then array[1]...) and then compare the incoming variable against all others (ex. if array[3] == array[1] || array[3] == array[2]) then stop, etc.)?

This article has been dead for over six months. Start a new discussion instead.