snowy desert dark & cloudy with celestial snow bridge

1.4 Java | Reading Input from Console

So in this tutorial I will be showing you how to make the Java program accept input from the user. In the previous tutorial, we had a variable radius that has its value fixed in the source code. So that if you wanted to use a different value, then you would have to modify the source code and recompile it. In order to skip this step, you can use the Scanner class for directly inputting data/values from the console.

Scanner Input

Java uses System.out to refer to the standard output device and System.in to refer to the standard input device. By default, the output device is the computer display and the input device is the keyboard.

You can perform console output by the println method.

On the other hand, console input is not directly supported in Java, but instead you can use the Scanner class to make an object read input from System.in, like so:

Note that you have to import the Scanner class before you can use it in your Java program. To do this, add this line of code to the beginning of your script:

Or you can let Java find the specific class to import by using a wild card:

Deconstruction

new tells Java to make an object.

new Scanner() tells Java to make an object of the Scanner type. (object is an instance of class)

new Scanner(System.in) tells Java to make an object of Scanner type that reads its input from the standard input device, the keyboard in this case.

Scanner input declares that input is a variable of Scanner type. (variable is a container for data)

So altogether Scanner input = new Scanner(System.in); makes a Scanner object that is assigned its reference to the variable input. To word it differently: The statement Scanner input = new Scanner(System.in); makes a variable called input, which when called makes an object that reads the input from the keyboard.

Deconstruction V2

new = make something. Scanner()= Read something. Scanner(System.in)= Read keyboard input. new Scanner(System.in) = make object that reads keyboard input.

Scanner input = new Scanner(System.in); = Make object that reads keyboard input, and store this action in the input container. When you want to do this action, call the input container.

Calling the Scanner | Using nextDouble()

You can invoke the nextDouble() method to read a double value as follows:

This statement reads a floating-point number from the keyboard and assigns the number to the radius variable.

If you want to prompt a user to enter a radius value to compute a circle’s radius, this is the how:

Notes:

  • The println method moves to the beginning of the next line after displaying the string. The print method stays on the same line as the string.
  • input.nextDouble() only accepts floating-point keyboard input. But the difference between FloatDouble is that Float stores 8 digits, but Double stores 16 digits.

Inputting Different Types of Data

For reading string input use _.next();
For reading byte input use _.nextByte();
For reading short input use _.nextShort();
For reading int input use _.nextInt();
For reading long input use _.nextLong();
For reading float input use _.nextFloat();
For reading double input use _.nextDouble();
For reading boolean input use _.nextBoolean();

Example: Computing the Average

Extra Notes: Key Definitions

As you can see in the statement above, an object may invoke its methods. To invoke a method on an object means to ask the object to perform a task.

Method = The task

Invoke a Method on Object = ask the object to perform a task

Object is an instance of class. = A class is a template for manufacturing objects. = A class is a blueprint that describes methods and variables in each object. = An object is something, while the class is simply the plans for making that something.

Motlle_crespellines represent the class and object relationship
This shows what “an object is an instance of a class” means.

What's Your Opinion?