Java Input and Output

Java Input and output are user interactions. Java Programming language allows to take input from user and display output.

Stream

In Java Programming language, user interaction takes place through streams. Stream is a sequence of data. They control the flow of data. There are three basic i/o streams:

  • Standard Output stream: System.out writes or displays the output on any standard output device like computer screen.
  • Standard Input stream: System.in reads user input from keyboard or any other standard input device.
  • Standard Error stream: System.err displays error message present in a program on a standard output device.
Java Input Output
Java Input Output Streams

Java Output

Java provides three built in methods to display output to the user on console. It uses System.out stream. These methods are:

  • System.out.println(); Prints the output in parenthesis and moves the cursor to next line.
  • System.out.print(); Simply prints the output in parenthesis.
  • System.out.printf(); Prints and provides string formatting.

All these methods send output to the output device (monitor screen). Here, system is a class and out is a public, static field that takes output data.

The below code shows how to display output using System.out.println() method.

import java.io.*;
class MyProgram {
    public static void main(String[] args)
    {
  
        System.out.println("Java ");
        System.out.println("Programming ");
        System.out.println("Language ");
    }
}

Note that each output id printed in new line. The output of the above code after compilation is:

Here is another example that prints using the method System.out.print().

Note that System.out.print() does not shift the cursor to the new line. So, all the outputs are printed in the same line:

import java.io.*;
class MyProgram {
    public static void main(String[] args)
    {
  
        System.out.print("Java ");
        System.out.print("Programming ");
        System.out.print("Language ");
    }
}

Here is another example that shows how to display formatted output using System.out.printf() method.

Instead of writing multiple print statements for each variable, programmer can write a generic print statement and add the values of variables in it using printf() method. In the code below, printf() method is used to create formatted string. Note that we use %d to access int type variable, %f to access float type variable and %s to access string.

import java.io.*;
public class MyProgram {
  public static void main(String[] args) {
    int varA = 41;
    float varB = 12;
    String varC = "Java";
    System.out.printf("The value of the integer variable is " +
                  "%d, the value of the float " +
                  "variable is %f, and the string variable " +
                  "is %s.", varA, varB, varC);
  }
}

The output of the above code after compilation is:

Java Input

Java provides various ways to take input from user. However, most commonly used method of getting java input is by using Scanner class object. The first step is to import the java.util package that contains the Scanner class. The command to import the package is:

Add this in the beginning of the code. After the package is imported, create an object of Scanner class. Later the object can be used to take input from user. The below code shows how to create an object input of scanner class:

The scanner class provides a list of methods to take input from the user depending on the type of user input. These methods and their descriptions are shown in the table below:

Sr.No.MethodDescription
1.nextInt()Takes integer type input from user.
2.nextLine()Takes String type input from user.
3.nextFloat()Takes Float type input from user.
4.nextDouble()Takes Double type input from user.
5.nextLong()Takes Long type input from user.
6.nextShort()Takes Short type input from user.
7.nextByte()Takes Byte type input from user.
8.nextBooleanTakes Boolean type input from user.
Java Input Methods

For example, the below code shows how to take an integer as java input from user using nextInt() method.

import java.util.Scanner;

class Example {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in);
    	
        System.out.print("Enter a number: ");
        int num = input.nextInt();
        System.out.println("You entered " + num);

        // closing the scanner object
        input.close();
    }
}

The output of the above code after compilation is: The user enters 50 from keyboard.

If text or a sentence is required as input from user, the method nextLine() method is used. For example, the below code takes multiple types of input from user:

In the below example, the code uses nextLine() to take user name, nextInt() to take user age and nextFloat() to take user weight as input:

import java.util.Scanner;

class Example {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your name, age and height:");

    // String input
    String name = input.nextLine();

    // Numerical input
    int age = input.nextInt();

    // Decimal input
    double height = input.nextFloat();

    // Output user input
    System.out.println("Name: " + name); 
    System.out.println("Age: " + age); 
    System.out.println("Height: " + height + "m"); 
  }
}

In this case, user inputs John Gable as name, 30 as age and 1.8 as height, as input. The output of the above code after compilation is:

If user enters wrong input then in this compiler gives an error. For example, if user enters alphabets in nextInt() input method, then compiler shows an error message saying something like “InputMismatchException”.