DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

Scanner Class in Java

What is Scanner Class?

  • It is used to take input from the user in Java.
  • It can read input from various sources like keyboard, files, or strings.
  • It is found in the java.util package so it need to be imported. import java.util.Scanner;

How to read various input ?

Scanner sc = new Scanner(System.in);

sc.nextInt() // reads int
sc.nextLong() // reads long
sc.nextDouble() // reads double
sc.nextFloat() // reads float
sc.nextShort() // reads short
sc.nextByte() // reads byte
sc.nextBoolean() // reads boolean (true/false)
sc.next() // Reads only one word,Stops at space,tab,newline
sc.nextLine() // reads full line Stops only when Enter key (newline) is pressed
sc.next().charAt(0); //to read a character
sc.nextBigInteger() // reads BigInteger
sc.nextBigDecimal() // reads BigDecimal

What is hasnext method?

  • It checkes, if the next token is of a specific type — useful to avoid errors.

  • Token means data separated by space, tab, or newline.

sc.hasNext() // true if any token available(word)
sc.hasNextInt() // true if next token is int
sc.hasNextLong() // true if next token is long
sc.hasNextDouble() // true if next token is double
sc.hasNextFloat() // true if next token is float
sc.hasNextShort() // true if next token is short
sc.hasNextByte() // true if next token is byte
sc.hasNextBoolean() // true if next token is boolean
sc.hasNextLine() // true if another line exists.Line means everything until newline (\n).
sc.hasNextBigInteger() // true if next token is BigInteger
sc.hasNextBigDecimal() // true if next token is BigDecimal

nextInt() + nextLine() Issue

int age = sc.nextInt();
String name = sc.nextLine();

Problem:
nextLine() gets skipped!

Why?
Because nextInt() leaves a newline (\n) in buffer.
Solution:
int age = sc.nextInt();
sc.nextLine(); // consume leftover newline
String name = sc.nextLine();

Why Should We Close Scanner?

sc.close();
Enter fullscreen mode Exit fullscreen mode
  • It releases system resources.

Example program :

import java.util.Scanner;

public class testInput {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter integer value");

        if(sc.hasNextInt()){
             int a = sc.nextInt();
                System.out.println("Value of a is "+a);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter byte value");

        if(sc.hasNextByte()){
            byte b = sc.nextByte();
            System.out.println("Value of b is "+b);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter short value");

        if(sc.hasNextShort()){
            short s = sc.nextShort();
            System.out.println("Value of s is "+s);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter long value");

        if(sc.hasNextLong())
        {
            long l = sc.nextLong();
            System.out.println("Value of l is "+l);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter float value");

            if(sc.hasNextFloat())
            {
                float f = sc.nextFloat();
                System.out.println("Value of f is "+f);
            }
            else
                System.out.println("incorrect input");

        System.out.println("Enter double value");

        if(sc.hasNextDouble())
        {
            double d = sc.nextDouble();
            System.out.println("Value of d is "+d);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter boolean value");

        if(sc.hasNextBoolean())
        {
            boolean bool = sc.nextBoolean();
            System.out.println("Value of bool is "+bool);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter a word");

        if(sc.hasNext())
        {
            String str = sc.next();
            System.out.println("Value of str is "+str);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter a string");

        if(sc.hasNextLine())
        {
            String str1 = sc.nextLine();
            System.out.println("Value of str1 is "+str1);
        }
        else
            System.out.println("incorrect input");

        System.out.println("Enter a character");

        char c = sc.next().charAt(0);
        System.out.println("Value of c is "+c);

        sc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Delimiter Methods :

  • By default Scanner splits input by whitespace. You can change this.

sc.delimiter() // returns current delimiter pattern
sc.useDelimiter(",") // split by comma

Scanner sc = new Scanner("Nanthini,Ammu,Kani");
        sc.useDelimiter(",");
        System.out.println(sc.next());
        System.out.println(sc.next());
        System.out.println(sc.next());
        System.out.println(sc.delimiter());


output :
Nanthini
Ammu
Kani
,

Enter fullscreen mode Exit fullscreen mode

Top comments (0)