Lesson 9 – Reading console input and import statements

Ok, In this short double lesson we are going to learn how to read input into our program from the console and use some new features from another java package using an import statement.

Downloads

To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

Reading basic input

The first thing we are going to look at is reading in a string from the console view in our IDE using a Buffered reader. This will allow us to interact with the program during it’s execution.

First create a new class called ‘ReadingInput’ with a main method.

Then, within the main method copy the following:

You will notice some compilation errors, ignore these for a moment. Next, paste the following code underneath:

So what about all those nasty errors? For that we need to learn the concept of Imports.

Imports

Up until now, everything we have learn’t has been using a collection of classes in a package called java.lang.

Forgot what a package is?

A package is a directory to hold and organise java classes (java files). Classes can be grouped together in packages in some sort of logical order.

An example of something we have used from the java.lang package is printing to our console:

However, the package java.lang is automatically imported into each java file(or class) that we create so we haven’t had to use imports yet.

The BufferedReader and the InputStreamReader used in the example above are in another package called java.io (IO = Input / Output). This package contains classes for reading and writing to and from a number of different sources such as files, network connections and you guessed it user input.

So for our example to work we will need to use the following import:

Imports are written between the package of the class we are in and the creation of the class itself. So the example so far looks like:

Once the import is declared we are able to access all the features of the IO package.

We will cover imports in more detail through the lesson series but for now just know that there are lots of packages which contain many classes that we can re-use depending on our needs. These are all part of the JRE.

If you would like to start looking at other packages check out the official java docs.

You’ll also notice that I have added throws IOException to the main method signature. This is simply saying that if the user inputs some unknown data, raise an error and close the program. Exceptions will be covered extensively in the intermediate tutorials.

Ok lets continue

Wether using Eclipse or IntelliJ have a look at the short video clips below on how to run the code and enter some input:

Eclipse
IntelliJ

So what are these new classes doing?

System.in

This is the standard input stream for any incoming data. This stream by default is open and ready to read input once declared. The most common use of this stream is to capture keyboard input or user input.

InputStreamReader

An InputStreamReader converts byte streams to character streams.

BufferedReader

Lastly, the BufferedRedaer reads text from a character-input stream (in out case an InputStreamReader), buffering characters so as to provide for the efficient reading of characters.

readLine() simply reads a line of text.

Another example of reading input

Lets take another example for reading in a string and converting it to an integer. Copy the following code to your example. You can replace the code under the buffered reader or just add it below the previous example:

This time when running the example enter a number. You will see that the input is converted to an Integer using the parseInt method which is inside the Integer class (The Integer class is part of the java.lang package so no need for any imports).

Although we are not covering exceptions until the next lesson series I would also encourage you to run this example and input a string which is not a number. You will see an example of a possible ‘Exception’.

For more examples see my course downloads

Menu