INTRODUCTION:
The given Java program generates a random password of a specified length and constructs an email address by combining the provided user ID with the generated password. It uses predefined character sets for numbers, letters (both uppercase and lowercase), and symbols to create a diverse pool of characters for the password. The program prompts the user to enter an email ID and the desired password length, validates the input, generates the password, and constructs the email address. Finally, it displays the generated password and email address on the console.
TOPICS COVERD USED IN JAVA:
-
NUMBERS,CHARACTERS, andSYMBOLS: These are three constants declared as strings, representing the character sets to be used for generating the password.NUMBERScontains digits from 0 to 9,CHARACTERScontains both uppercase and lowercase letters, andSYMBOLScontains a set of special symbols. -
generatePasswordmethod: This method takes an integerlengthas a parameter and returns a randomly generated password of the specified length. It concatenates theCHARACTERS,NUMBERS, andSYMBOLSstrings into thecharactersstring. It then uses aSecureRandomobject to generate random indices within the bounds of thecharactersstring and appends the corresponding characters to aStringBuilderobject. Finally, it returns the generated password as a string. -
generateEmailmethod: This method takes two strings,userandpassword, as parameters and constructs an email address by appending theuserstring with thedomainstring (which is hardcoded as "@gmail.com"). The resulting email address is returned as a string. -
mainmethod: This is the entry point of the program. It prompts the user to enter an email ID and the desired length for the password using aScannerobject. It validates the user input and checks for empty input or negative password length. If the input is valid, it generates a random password using thegeneratePasswordmethod and constructs an email address using thegenerateEmailmethod. Finally, it prints the generated password and email address to the console.
The code demonstrates concepts such as random number generation, string manipulation, user input validation, and basic console I/O operations in Java.
PACKAGES: import java.security.SecureRandom; import java.util.Scanner;