I am writing a simple program where the user enters a name and an integer. If the integer was not already used, the name goes into an array. My problem is that I can’t seem to figure out how to check the integer when it is entered to see if it was already used (duplicated). I tried putting the integers into an array, but could not get it to work. Please keep in mind that I am a complete novice. So in the code below, after the user enters a name, he/she enters a time (integer) and if the time was not already used, it is valid; if it was used, I’ll need to throw an exception. So far, all I have is the code working for the name (adding it to the array).
public void enterName()
{
Scanner keyboard = new Scanner(System.in);
for(i = 0; i < arrayNames.length; i++)
{
System.out.println("Please enter your name.");
names = keyboard.nextLine();
arrayNames[i] = names;
System.out.println("name is in");
}
System.out.println("array is full");
}
If you absolutely must use an array, here’s how to check for duplicate entries:
EDIT
The expression
for (int number : numbers)basically meansfor each int of the int array do. Internally, an Iterator is created, which is then used to iterate over the elements of the array (or collection).But for the moment, you can think of it being (semantically) the same as: