-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversityStringValidator.java
More file actions
45 lines (37 loc) · 1.63 KB
/
Copy pathUniversityStringValidator.java
File metadata and controls
45 lines (37 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
// 1. Define the Custom Exception
class NoMatchFoundException extends Exception {
public NoMatchFoundException(String message) {
super(message); // Pass the custom message to the parent Exception class
}
}
// 2. Main Driver Class (PascalCase)
public class UniversityStringValidator {
// Method that validates the string and declares that it throws our custom exception
public static void checkString(String input) throws NoMatchFoundException {
// Check if the string is NOT equal to "University" (Case-sensitive)
if (!input.equals("University")) {
throw new NoMatchFoundException("Error: The provided string is not equal to 'University'.");
} else {
System.out.println("Success! The string perfectly matches 'University'.");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- University String Validator ---");
System.out.print("Please enter a string: ");
String userInput = scanner.nextLine();
System.out.println("\n--- Validation Result ---");
// 3. Use try-catch block to handle the custom exception
try {
checkString(userInput);
} catch (NoMatchFoundException e) {
// This block executes if the exception is thrown
System.out.println("Caught Custom Exception: " + e.getMessage());
} finally {
System.out.println("-------------------------");
System.out.println("Validation process completed.");
}
scanner.close();
}
}