Input in Java using Scanner

  • Java
  • 4 mins read

In this article, you will learn how to take input at program execution time using the Scanner class methods in Java.

Java provides us with several classes for taking input from the console.

The most used class nowadays is Scanner.

Java Scanner Class

The Scanner class is defined in java.util package. A package is a collection of several classes, which helps us in reusing them.

We have to import java.util.Scanner at the top of our java source file in the program to use the Scanner class.

To use the Scanner class methods to read data from the keyboard, we must create an object of Scanner.

To create an object of Scanner we use the following syntax:

Scanner kb=new Scanner(System.in);

In the above statement kb is an identifier, you can use any identifier of your choice. The “new” is the operator that is used for creating objects with the help of a constructor in java. System.in is a predefined object representing the standard input device, i.e. keyboard.

The Scanner class provides the following methods to read data:

Value to be readMethod of scanner
bytenextByte()
shortnextShort()
intnextInt()
longnextLong()
floatnextFloat()
doublenextDouble()
booleannextBoolean()
Single word stringnext()
Multiword stringnextLine()

The Scanner class does not provide any method for reading characters directly. Although we can use a single word and extract the first character from the string.

Taking integer value input in Java

Scanner kb=new Scanner(System.in);
int a=kb.nextInt();

In the above code, we created a Scanner class object, then we read an integer value using the next() method with kb, and stored the value into the variable a.

Taking double value input in Java

Scanner kb=new Scanner(System.in);
double d=kb.nextDouble();

In the above code, we created a Scanner class object, then we read a double value using the nextDouble() method with kb, and stored the value into the variable d.

Taking character value input in Java

Scanner kb=new Scanner(System.in);
char c=kb.next().charAt(0);

In the above code, we created a Scanner class object, then we read a string value using the next () method with kb, and finally extracted the first character from entered string using the charAt() method provides by the String class and storing the value into the variable c.

For example: if you input tarun then t will be stored into the variable c.

Taking single word string input using Scanner

Scanner kb=new Scanner(System.in);
String name=kb.next();

In the above code we read a string value using the next () method with kb, and store the value into the variable name.

For example: if you input tarun then tarun will be stored into the variable name. Remember that next() can read only a single word, for example, if you enter tarun verma then only tarun will be stored into the variable name.

Taking multi-word (line) string input

Scanner kb=new Scanner(System.in);
String lyrics=kb.nextLine();

In the above code, we read a line using the nextLine() method with kb and store the value into the variable lyrics.

For example: if you input Smooth like butter, like a criminal undercover then Smooth like butter, like a criminal undercover will be stored into the variable lyrics.

Below is a complete example to take input in Java using the Scanner class:

import java.util.Scanner;  

class Main {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    String userName;
    
    System.out.println("Enter Name"); 
    userName = kb.nextLine();   
       
    System.out.println("Name is: " + userName);        
  }
}
Related Posts: