Java Console - Page 2 0f 5

Output :
Picture
Explanation :

Since I have access to console object the output was shown as “The Console object is java.io.Console@42e816”.  If you don’t have access to the Console object you may get output as “The Console object is null”.

Before proceeding to the next level first make sure that you have access to the System.console() Object.
Console class has so many methods but which all methods are required for SCJP 1.6 exam?

You need to know only 3 methods to give SCJP 1.6 exam. ( very little isn’t it ?). The methods are as follows.


  1. readLine()
  2. readPassword()
  3. format().

The method definitions are as follows : (For complete definitions of the methods click here


Method # 1. readLine

public String readLine()

Description : Reads a single line of text from the console.

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.

Throws:  IOError - If an I/O error occurs.

What you have to remember here is that readLine() method can read a single line from the command line and return that line as string.

Method # 2.   format 
public Console format(String fmt,Object... args)

Writes a formatted string to this console's output stream using the specified format string and arguments.

String fmt : it you know ‘C’ Programming then it is very easy to find out

public Console format(“The value of variable i is %d”,i);

The output of the above is “The value of variable I is 0”. Simply you have to replace %d with the value of corresponding integer.

Now we’ll practically see the implementation of these methods:
 <<<Previous   ||  Next >>>


ALL about SCJP = SCJP 1.6, mock, certification, scjp 1.5 , scjp questions, scjp books, scjp hand written notes, scjp dumps, scjp trainings, scjp video tutorials, scjp simulators etc.