7.Write a Java program to create a method that takes a string as input and throws an exception if the string does not contain vowels.
💡Code:
import java.util.Scanner;
public class VowelChecker7078 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
try {
checkForVowels(input);
System.out.println("The string contains vowels.");
} catch (NoVowelsException e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
scanner.close();
}
public static void checkForVowels(String str) throws NoVowelsException {
if (!str.matches(".*[aeiouAEIOU].*")) {
throw new NoVowelsException("No vowels found in the string.");
}
}
}
class NoVowelsException extends Exception {
public NoVowelsException(String message) {
super(message);
}
}
📸Output :