Console
public class ConsoleInputExample {
public static void main(String[] args) {
// Obtain a console object
Console console = System.console();
if (console == null) {
System.out.println("Console is not available. Please run this program from the command line.");
System.exit(1);
}
// Read a string input
String name = console.readLine("Enter your name: ");
// Read an integer input
int age = Integer.parseInt(console.readLine("Enter your age: "));
// Read a double input
double salary = Double.parseDouble(console.readLine("Enter your salary: "));
// Read a boolean input
boolean isStudent = Boolean.parseBoolean(console.readLine("Are you a student? (true/false): "));
// Print the inputs
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Is Student: " + isStudent);
}
}Last updated