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.

Table of Contents

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:

Scanner input = new Scanner(System.in);

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:

import java.util.Scanner;

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

import java.util.*;

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:

double radius = input.nextDouble();

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:

import java.util.Scanner; //Scanner is in the java.util package

public class ComputeAreaWithConsoleInput {
    public static void main(String[] args) {
        // make a Scanner object to read from keyboard
        Scanner input = new Scanner(System.in);
        
        // Prompt the user to enter a radius
        System.out.print("Enter a number for radius: ");
        double radius = input.nextDouble(); //Store keyboard input in the radius variable as a double
        
        // Compute area
        double area = radius * radius * 3.14;
        
        // Display results
        System.out.println("The area for the circle of radius " + radius + "is " + area);
    }
}

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

import java.util.Scanner; //Scanner is in the java.util package

public class ComputeAverage {
    public static void main(String[] args) {
        // Make a Scanner object
        Scanner input = new Scanner(System.in);

        //Prompt the user to enter three numbers
        System.out.print("Enter three numbers: ");
        double number1 = input.nextDouble();
        double number2 = input.nextDouble();
        double number3 = input.nextDouble();

        //Compute average
        double average = (number1 + number2 + number3)/3;

        //Display results
        System.out.println("The average of " + number1 + " " + number2 + " " + number3 + " is " + 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?