Q:

Write a Java program to input and display your password

1

Write a Java program to input and display your password


Expected Output

Input your Password:                                                    
Your password was: abc@123

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.io.Console;
public class Example42 {
	public static void main(String[] args) {
		Console cons;
		if ((cons = System.console()) != null) {
			char[] pass_ward = null;
			try {
				pass_ward = cons.readPassword("Input your Password:");
				System.out.println("Your password was: " + new String(pass_ward));
			} finally {
				if (pass_ward != null) {
					java.util.Arrays.fill(pass_ward, ' ');
				}
			}
		} else {
			throw new RuntimeException("Can't get password...No console");
		}
	}
}

Sample Output:

Input your Password:                                                    
Your password was: abc@123

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now