Scanner in Java

Use of Scanner in Java language

We have discussed the use of Scanner in Java language on this page. The Scanner Class in Java is a class, 
which is part of the Java Utilities Package, that gives users a means to input data that the program can utilise to guide its behaviour.
We’ll discover how to use Scanner to read input from a file, String or console.

Scanner In Java

Use of Scanner Class in Java language

Scanner is a component of the java.util package, hence we must import java.util first in order to use the scanner class in Java.util.Scanner package at the very top of our Java program.

  import java.util.*;
  or
  import java.util.Scanner;

Importing File

While importing a file in Java we need to import the .File class

  import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;

  public class Main {

    public static void main(String args[]) throws FileNotFoundException {

        File text = new File("C:/Path of the file");

        Scanner sc = new Scanner(text);

        int ln = 1;

    while(sc.hasNextLine()){
        String line = sc.nextLine();
        System.out.println("line " + ln + " :" + line);
        ln++;
    }
  }
}

Taking input from the Console

Now let’s have a look on taking input from the user through console:

  1. Firstle, we need to import the java.util.Scanner class or the whole java.util package.
  2. Create an object of Scanner Class.
  3. Initialize an input variable that can be int, char, or String.
  4. Perform the instructions you want to do.

Run

import java.util.Scanner;   //Importing Class

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    //Creating object of scanner class
        System.out.println("Hello! Can you please tell me your name?");
        String obj = sc.nextLine();    //Variable Initialization
        System.out.println("Nice to see you "+obj);
    }
}

Some Commonly used Methods are:

  • hasNext() This will return true if the scanner has another token in the input.
  • nextBoolean() This method returns the result of scanning the following token in the input as a Boolean.
  • nextByte() The next token in the input will be scanned as a byte.
  • nextDouble() The following token in the input will be scanned as a double.
  • nextFloat() The following token in the input will be scanned as a float.
  • nextInt() The next token in input will be scanned as an integer using the this method.
  • nextLong() This Scans the following token of the input as a long.
  • nextShort() The next character in input will be scanned as a short using the this method.
  • close() Using this method closes the scanner.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription