-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInput_Using_Console.java
More file actions
39 lines (31 loc) · 1.48 KB
/
Input_Using_Console.java
File metadata and controls
39 lines (31 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Program: Input Using Console
* Description: A Java program to demonstrate user input using the Console class.
* Note: System.console() typically requires the program to be run from a command line,
* not from within typical IDE consoles/terminals.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.io.Console;
public class Input_Using_Console {
public static void main(String args[]) {
// Creating object of the Console class
Console console = System.console();
System.out.println("---------------------------------------------");
System.out.println(" Console Input Demo");
System.out.println("---------------------------------------------");
if (console == null) {
System.out.println("No console available (Running in basic IDE terminal?)");
return;
}
// Read a string and then display it
String name = console.readLine("Enter you name: ");
System.out.println("Hello, " + name);
// Read password (masked input) and then display it
char[] passwordArray = console.readPassword("Enter the password: ");
// Converting char array into string
String password = String.valueOf(passwordArray);
System.out.println("Password accepted: " + password); // Displaying just to verify
System.out.println("---------------------------------------------");
}
}