Certain authentication system requires the user to enter a password which is an integer. The authentication system should display "Invalid integer. Enter a valid input.", if the password entered is not a valid integer. This process should be repeated until the user enters a valid integer input. If the password is a valid integer, the authentication system checks if the integer is prime or not. If the password is prime, it displays "Authenticated," otherwise it displays "Unauthenticated”. Write a Java program for the above with proper exception handling.Input-Output Form 1:Enter a valid integer password: 12.3Output: Invalid integer. Enter a valid input.Enter a valid integer password: 12.7Output: Invalid integer. Enter a valid input.Enter a valid integer password: 12Output: Unauthenticated
Question
Certain authentication system requires the user to enter a password which is an integer. The authentication system should display "Invalid integer. Enter a valid input.", if the password entered is not a valid integer. This process should be repeated until the user enters a valid integer input. If the password is a valid integer, the authentication system checks if the integer is prime or not. If the password is prime, it displays "Authenticated," otherwise it displays "Unauthenticated”. Write a Java program for the above with proper exception handling.Input-Output Form 1:Enter a valid integer password: 12.3Output: Invalid integer. Enter a valid input.Enter a valid integer password: 12.7Output: Invalid integer. Enter a valid input.Enter a valid integer password: 12Output: Unauthenticated
Solution
Here is a Java program that implements the described behavior:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
try {
System.out.println("Enter a valid integer password: ");
String input = scanner.nextLine();
int password = Integer.parseInt(input);
if (isPrime(password)) {
System.out.println("Authenticated");
break;
} else {
System.out.println("Unauthenticated");
break;
}
} catch (NumberFormatException e) {
System.out.println("Invalid integer. Enter a valid input.");
}
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
This program prompts the user to enter a password. If the input is not a valid integer, it catches the NumberFormatException and prompts the user to enter a valid integer again. If the input is a valid integer, it checks if the number is prime using the isPrime method. If the number is prime, it prints "Authenticated" and exits the loop. If the number is not prime, it prints "Unauthenticated" and exits the loop.
Similar Questions
Develop a Java program that creates a simple GUI application for a passwordvalidator. The program should allow users to input a password and check ifit meets certain criteria.
Write a Java program that implements a password validator using a custom exception InvalidPasswordException. The program should have a method validatePassword that takes a string as an argument representing a password and checks its validity based on the following rules: The password should be at least 12 characters long.The password should be a combination of uppercase and lowercase letters, numbers, and symbols.If the password does not meet these criteria, the validatePassword method should throw an InvalidPasswordException with an appropriate error message. Provide 5 sample inputs and their corresponding expected outputs to demonstrate the functionality of the validatePassword method.Expected Output:For a valid password, the output should be "Password is valid."For a password that is too short, the output should be "Invalid Password: Password should be at least 12 characters long."For a password without required characters, the output should be "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."Sample Input:"Strong@Password123"Sample Output:Password is valid.
Write a java program to print the division of two integers received from the user. Handle exceptions using nested try-catch blocks that checks if both numbers received are valid integers and print the division value if no exceptions occur during the arithmetic operation. Input 1:Enter Number 1: 10Enter Number 2: 5Output 1:2
Write a Java program that takes a positive integer as input and performs prime factorization using a while loop. The program should display the prime factors of the given number.Ensure the program handles invalid input gracefully, providing appropriate messages
Create a Java program that takes two integer inputs 'a' and 'b' from the user and calculates 'a/b'. Implement exception handling to catch potential arithmetic and input mismatch errors, providing appropriate error messages for each case.Your task is to write a Java Program to Handle Arithmetic Exceptions and InputMisMatchExceptions.Note: Refer to the visible test cases for more clarity.Constraint:0≤𝑎≤1020≤𝑏≤102Input Format: The input consists of two numbers each in a new line (integer).Output Format: The output represents the result of the division of the two numbers or any error that occurred while dividing.Sample Test CasesTest Case 1:Expected Output:Enter·the·value·of·'a':·1Enter·the·value·of·'b':·0Arithmetic·Exception:·/·by·zeroTest Case 2:Expected Output:Enter·the·value·of·'a':·sInput·Mismatch·Exception:·Please·enter·valid·integer·values.Test Case 3:Expected Output:Enter·the·value·of·'a':·50Enter·the·value·of·'b':·4Result·of·a/b:·12
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.