Java Program: User input validation for duplicate integers
Write a Java program that reads a list of integers from the user and throws an exception if any numbers are duplicates.
Sample Solution:
Java Code:
import java.util.*;
public class Duplicate_Number_Check {
public static void main(String[] args) {
try {
List < Integer > numbers = readNumbersFromUser();
checkDuplicates(numbers);
System.out.println("No duplicate numbers!");
} catch (Duplicate_Number_Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public static List < Integer > readNumbersFromUser() {
List < Integer > numbers = new ArrayList < > ();
Scanner scanner = new Scanner(System.in);
System.out.print("How many numbers do you want to input? ");
int count = scanner.nextInt();
System.out.println("Input the integers:");
for (int i = 0; i < count; i++) {
int num = scanner.nextInt();
numbers.add(num);
}
scanner.close();
return numbers;
}
public static void checkDuplicates(List < Integer > numbers) throws Duplicate_Number_Exception {
Set < Integer > uniqueNumbers = new HashSet < > ();
for (int num: numbers) {
if (uniqueNumbers.contains(num)) {
throw new Duplicate_Number_Exception("Duplicate number found: " + num);
}
uniqueNumbers.add(num);
}
}
}
class Duplicate_Number_Exception extends Exception {
public Duplicate_Number_Exception(String message) {
super(message);
}
}
Sample Output:
How many numbers do you want to input? 5 Input the integers: 1 2 3 4 5 No duplicate numbers!
How many numbers do you want to input? 6 Input the integers: 1 2 3 3 4 5 Error: Duplicate number found: 3
Explanation:
In the above exercise,
- The program includes three classes: Duplicate_Number_Check, Duplicate_Number_Exception, and the main class.
- The main method is defined in the Duplicate_Number_Check class and serves as the entry point for the program.
- In the main method, we call the readNumbersFromUser method to read a list of integers from the user. We store them in a List<Integer> called numbers.
- Next we call the checkDuplicates method, passing the numbers list as an argument. If there are duplicate numbers in the list, a Duplicate_Number_Exception is thrown.
- If no exception is thrown, we print the message "No duplicate numbers!".
- The readNumbersFromUser method prompts the user to enter the number of integers they want to input and reads the integers from the user. The method returns a list of entered numbers.
- The checkDuplicates method takes a list of integers as a parameter and checks for duplicates using a Set<Integer> called uniqueNumbers. Upon iterating over each number in the list, a Duplicate_Number_Exception is thrown if that number already exists in the uniqueNumbers set.
- The Duplicate_Number_Exception class is a custom exception class that extends the base Exception class. Using the super keyword, it passes a message parameter to the constructor of the superclass.
- IDuplicate_Number_Exceptions are caught in the main method, an appropriate error message is printed.
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: File reading and empty file exception handling.
Next: String input validation for vowels.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics