I am working on this project that wants me to write a program that inputs 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. The only part I am confused about is how to go about checking for duplicate values that the user may enter. And IF the user does input a duplicate value, it should not be stored again.
In addition, the value entered should be printed out after it is entered along side the value that have been previously entered by the user such as: 23 23 45 23 45 67 23 45 67 12 and so on.
I am still fairly new at java programming so any help would be great.
import java.util.*;
public class NumberArray
{
public static void main(String[] args)
{
// declare an array with 20 elements
Scanner input = new Scanner( System.in );
int num[] = new int[20];
int index = 0;
int enteredNumbers = 0;
while( enteredNumbers < num.length )
{
System.out.print( "\nEnter number: ");
int numberInput = input.nextInt();
if (numberInput >= 10 && numberInput <= 100)
{
num[index] = numberInput;
System.out.println("Number stored.");
}
//Check if numbers repeat--if not add to array
else if(num[index] == numberInput)
{
System.out.println("Duplicate value entered!\n");
}
else
{
System.out.println("Invalid Number, enter within range.\n");
}
// increment number of entered numbers
System.out.print(num[index] + " ");
System.out.println();
enteredNumbers++;
index++;
}
}
}