Kotlin Console Object

The System.console() method provides an access point for the Console object, which is used to read input from a character input device like the keyboard and prints to character display device like the screen’s display. If the JVM is started indirectly or as a background process, the method will return null. The Console object is useful when creating CLI applications.

The class provides a way to both read character input and write character output. Keep in mind that character output may be less important than input. Kotlin already provides its own print() and println() functions for writing to the standard out. The console class does make it easy to read input from the keyboad since we do not need to work about working with System.in directly.

Let’s take a look at an example where we simply echo what’s inputted to the keyboard.

import java.io.Console

fun main(args : Array<String>){
    val console : Console? = System.console()
    when (console){
        null -> {
            println("Running from an IDE...")
        }
        else -> {
            while (true){
                //Read a line from the keyboard
                val line = console.readLine("What does Bob say? ")
                if (line == "q"){
                    return
                }
                console.printf("Bob says: %s\n", line)
            }
        }
    }
}

The program is really simple. We make a console variable, that is nullable. This step is critical because System.console() can return null. Our next operation is to check if the console object is null. When console is null, we exit the program. Otherwise, we enter a loop that continues until the user enters “q” to quit.

The console.readLine() method returns a line inputted from the keyboard. In other words, it will return everything typed until the user pressed the enter or return key. To print the output, we use printf() to print a formatted output.
We could have used Kotlin’s print() function also.

Common Console Methods

reader() : Reader

This method returns a Reader object reference that can be used for low-level read operations.

writer() : PrintWriter

Returns a PrintWriter object reference that can be used for low-level output operations.

readLine() : String

Returns a line of text inputed from the console. The overloaded version accepts a String that serves as a command prompt.

readPassword() : CharArray!

This method disablese echoing so that a user can type a password in private. The overloaded version takes a String that is used as a command prompt.

format(fmt : String, varArgs args : Any) : Console

This method writes the format string an it’s argments to the output.

printf(fmt : String, varArgs args : Any) : Console

Does same thing as format.

flush() : void

This method flushes the buffer and forces all output to be written immediately.

References

See https://docs.oracle.com/javase/8/docs/api/java/io/Console.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s