GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - Console Class
Selected Reading
Java - Console ClassIntroductionThe Java Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. Class declarationFollowing is the declaration for Java.io.Console class − public final class Console
extends Object
implements Flushable
Class methods
Methods inheritedThis class inherits methods from the following classes −
Important NoteSystem.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution. Example - Ensuring Immediate Output Before a DelayThe following example shows the usage of Java Console flush() method. This example demonstrates using flush() to display text immediately before executing a time-consuming operation. ConsoleDemo.javapackage com.tutorialspoint;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console console = System.console();
if (console != null) {
// Print without newline and flush to ensure it's displayed immediately
console.writer().print("Processing...");
console.flush(); // Forces immediate display of "Processing..."
// Simulating a long-running task
try {
Thread.sleep(3000); // Simulate 3 seconds delay
} catch (InterruptedException e) {
e.printStackTrace();
}
console.writer().println(" Done!"); // Completes the message
console.flush();
} else {
System.out.println("No console available.");
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Processing... Done! Explanation
Example - Formatting and Displaying User InputThe following example shows the usage of Java Console format(String fmt, Object... args) method. This program prompts the user for their name and age, then formats and displays the input using format(). ConsoleDemo.javapackage com.tutorialspoint;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console console = System.console(); // Get Console object
if (console == null) {
System.out.println("No console available.");
return;
}
String name = console.readLine("Enter your name: ");
int age = Integer.parseInt(console.readLine("Enter your age: "));
console.format("Hello, %s! You are %d years old.%n", name, age);
}
}
Console InteractionLet us compile and run the above program, this will produce the following result − Enter your name: Anil Enter your age: 32 Hello, Anil! You are 32 years old. Explanation
Example - Printing Formatted User Details Using ConsoleThe following example shows the usage of Java Console printf(String fmt, Object... args) method. This program prompts the user for their name and age and prints them in a formatted way using printf(). ConsoleDemo.javapackage com.tutorialspoint;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console console = System.console(); // Get Console object
if (console == null) {
System.out.println("No console available.");
return;
}
String name = console.readLine("Enter your name: ");
int age = Integer.parseInt(console.readLine("Enter your age: "));
console.printf("Hello, %s! You are %d years old.%n", name, age);
}
}
Console InteractionLet us compile and run the above program, this will produce the following result − Enter your name: Kumar Enter your age: 43 Hello, Kumar! You are 43 years old. Explanation
Advertisements
| ||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |