Write a program to accept user name and password from the user.
For handling exception implement necessary catch blocks and print the messages accordingly.
Sample Input1
Arun
abcd123
SampleOutput1
Correct
Sample Input2
Arun
abcdefgh
SampleOutput2
No digit!
Sample Input3
Arun
abc1
SampleOutput3
Tooshort!
import java.util.Scanner;
// Other imports go here
// Do NOT change the class name
class Main
{
// Custom exception for a short password
static class ShortPasswordException extends Exception {
public ShortPasswordException(String message) {
super(message);
}
}
// Custom exception for missing digits in the password
static class NoDigitException extends Exception {
public NoDigitException(String message) {
super(message);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String user = sc.next();
String password = sc.next();
try {
// Check if password contains at least one digit
boolean hasDigit = false;
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) {
hasDigit = true;
break;
}
}
if (!hasDigit) {
throw new NoDigitException("No digit!");
}
// Check if password is less than 6 characters
if (password.length() < 6) {
throw new ShortPasswordException("Too short!");
}
// If no exception, the password is correct
System.out.println("Correct");
} catch (ShortPasswordException e) {
System.out.println(e.getMessage());
} catch (NoDigitException e) {
System.out.println(e.getMessage());
}
}
}