Basic input using Scanner class in Java

Write a java program to perform basic input using Scanner class. In this article I will explain how to input all basic data types using Scanner class in Java. We will learn how to use java.util.Scanner class to input data from user.

Example
Input

Enter charater value: c
Enter integer value: 10

Output

You entered charater : c
You entered integer: 10

In previous post we learned basics of Java. We learned basic program structure and executed our first Hello world Java application. In this example we will continue further and learn basic input output using Scanner class in Java.

Required knowledge

java.util.Scanner, Stander InputStream(System.in) and outputStream (System.out)

Unlike C programming language, Java provides several built-in classes for reading input from user. For example, java.io.BufferedReader, java.io.Console, java.util.Scanner etc.

To make your Java learning journey easy, first I will discuss about java.util.Scanner class.

How to read input using Scanner class

Java has a built-in Scanner class, to perform basic input output on all primitive data types. It is defined under java.util package.

Scanner class provides methods to read input of all primitive data types. It uses regular expressions to break its inputs into tokens.

How to use Scanner class

  • Import Scanner class at the top of your Java program. We must import all classes that we use in our Java program. Hence write an import statement at top, say import java.util.Scanner.
  • Create an object of Scanner class. Say Scanner in = new Scanner(System.in);.
  • Use Scanner class methods as per your data type to read input from user. Say to read integer from user use in.nextInt();, similarly use others.

Scanner class input methods

  • next() – Finds and returns the next string from input stream.
  • nextLine() – Return line from from input.
  • nextByte() – Return byte read from input.
  • nextShort() – Return short read from input.
  • nextInt() – Return integer read from input.
  • nextLong() – Return long read from input.
  • nextFloat() – Return float read from input.
  • nextDouble()– Return double read from input.

Program to read input using Scanner class

/**
 * Java program to read input using Scanner class
 */
import java.util.Scanner;

public class BasicIO {

    public static void main(String[] args) {
        
        /*
         * Declare all basic data types variable
         */
        char 	charVal;
        
        byte 	byteVal;
        short 	shortVal;
        int 	intVal;
        long 	longVal;
    
        float 	floatVal;
        double 	doubleVal;
        
        String  strVal;
        

        /* Create Scanner object */
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter charater value: ");
        charVal = in.next().charAt(0);
        
        System.out.print("Enter byte value: ");
        byteVal = in.nextByte();
        
        System.out.print("Enter short value: ");
        shortVal = in.nextShort();
        
        System.out.print("Enter integer value: ");
        intVal = in.nextInt();
        
        System.out.print("Enter long value: ");
        longVal = in.nextLong();
        
        System.out.print("Enter float value: ");
        floatVal = in.nextFloat();
        
        System.out.print("Enter double value: ");
        doubleVal = in.nextDouble();
        
        in.nextLine();   // Skip extra newline character
        
        System.out.print("Enter a String: ");
        strVal = in.nextLine();
        

        in.close();      // Close input stream (Scanner)

        

        /*
         * Print value of all variable
         */
        System.out.println("You entered character: " + charVal);
        System.out.println("You entered byte: "      + byteVal);
        System.out.println("You entered short: "     + shortVal);
        System.out.println("You entered integer: "   + intVal);
        System.out.println("You entered long: "      + longVal);
        System.out.println("You entered float: "     + floatVal);
        System.out.println("You entered double: "    + doubleVal);
        System.out.println("You entered String: "    + strVal);
    }

}

Note: System.out.print() method print string in same line. Whereas System.out.println() method print string and moves cursor to new line.

You might be wondering why I have used extra in.nextLine(); at line no 56. This is because nextDouble() read next double value from input stream ignoring the new line character. Hence to eat that new line character I have used a dummy nextLine() function.

Output

Enter charater value: c
Enter byte value: 127
Enter short value: 32767
Enter integer value: 2147483647
Enter long value: 9223372036854775807
Enter float value: 1.40239846
Enter double value: 4.9406564584124654
Enter a String: Codeforwin

You entered character: c
You entered byte: 127
You entered short: 32767
You entered integer: 2147483647
You entered long: 9223372036854775807
You entered float: 1.4023985
You entered double: 4.940656458412465
You entered String: Codeforwin

Happy coding 😉