Write a java program that reads 10 numbers from the user, but does not allow the user to enter duplicates.

Question:
Write a java program that reads 10 numbers from the user, but does not allow the user to enter duplicates.

Code:
import java.util.*;
 
public class TenNumbers {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);
 
    Set set = new TreeSet();
    do {
      System.out.print("Enter number: ");
      try {
        int i = sc.nextInt();
        if (set.contains(i)){
          System.out.println("Duplicate.");
        } else {
          set.add(i);
        }
      } catch (InputMismatchException e) {
         System.out.println("Not a number.");
         sc.nextLine();
      }
 
    } while (set.size()<10);
 
    System.out.println("set: " + set);
  }
}

Output:
$ java TenNumbers
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 4
Enter number: 5
Enter number: 6
Enter number: 7
Enter number: 8
Enter number: 9
Enter number: ten
Not a number.
Enter number: 9
Duplicate.
Enter number: 10
set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]