Jasa SEO,Jasa Pembuatan Website.
19 January, 2020
What is scanner in java

What is scanner in java ? How to get user input in java?

In this tutorial we will learn about  What is scanner in java and How to get user input in java

Java is Powerful programming language having huge set of community. It is easy to learn over C and C++. Java have almost same programming structure as C and C++.

Let’s discuss about the Scanner class in java to get the user input.

What is scanner in java ?

Basically the Scanner class is used to get the input from the user, and  java.util package contains Scanner class.

Java Scanner Class Declaration

public final class Scanner
extends Object   
    implements Iterator<String>    

How to get Java Scanner

To get the instance of Scanner in Java that reads input from the user, In the constructor of Scanner class ,we need to pass the input stream (System.in) . For Example:

Scanner in = new Scanner(System.in);

To get the instance of  Scanner in Java which parses the strings,  In the constructor of Scanner class ,we need to pass the strings. For Example:

Scanner in = new Scanner(“Hello Proline Coders”);  

To use the Scanner class we have to create the Object of the Scanner class. The basic method in the Scanner class is nextLine() method, it is used to read and get the user input values.

Reference Code

import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input

Input Types

In the example above, To read the String I used the nextLine() method, Following are the other types, look below at the table:

nextBoolean()Reads a boolean value from the user
nextByte()Reads a byte value from the user
nextDouble()Reads a double value from the user
nextFloat()Reads a float value from the user
nextInt()Reads a int value from the user
nextLine()Reads a String value from the user
nextLong()Reads a long value from the user
nextShort()Reads a short value from the user

In following example, I use different methods to read user input data of multiple types:

Reference Code

import java.util.Scanner;

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

    System.out.println("Enter name, age and salary");

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

    // Numerical input
    int age = myObj.nextInt();
    double salary = myObj.nextDouble();

    // Output input by user
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);


Reference Example

Let’s see another example of Java Scanner where I get the input from the user. Here,I am asking for a string through hasNextBoolean() method.

import java.util.*;
public class ScannerClassExample {
   public static void main(String args[]){
      String str = "Hello/This is ProlineCoders / My name isAbhishek.";
//Create scanner with the specified String Object
      Scanner scanner = new Scanner(str);
      System.out.println("Boolean Result: "+scanner.hasNextBoolean());
//Change the delimiter of this scanner
//Printing the tokenized Strings
      System.out.println("---Tokenizes String---");
//Display the new delimiter
      System.out.println("Delimiter used: " +scanner.delimiter());


Boolean Result: false
---Tokenizes String---
This is Proline Coders
My name is Abhishek.
Delimiter used: /OUTPUT

Refer from javatpoint


Leave a Reply

Your email address will not be published. Required fields are marked *