Java Console readLine() Example
Console class readLine() method example. This example shows you how to use readLine() method.
Syntax is : public String readLine()
Method reads a single line of text from the console and returns a string containing the line read from the console, not including any
line-termination characters, or null if an end of stream has been reached.
Here is the code.
/**
* @(#) ReadLineConsole.java
* A class representing use of method readLine() of Console class in
java.io Package.
* @Version 11-June-2008
* @author Rose India Team
*/
import java.io.*;
public class ReadLineConsole {
public static void main(String[] args) {
//Create a console object.
Console objConsole = System.console();
if (objConsole != null) {
// readLine method call.
String userName = objConsole.readLine("Enter username : ");
System.out.println("You have entered : " + userName);
}
}
} |
Output of the program.
Enter username : mahendra
You have entered : mahendra |
|