Java Scanner Class

java dev.to

Java Scanner Class Explained

If you're learning Java, one of the first things you'll need is taking input from the user. That's where the Scanner class comes in.

In this post, you'll learn:

  • What is Scanner?
  • Why do we use it?
  • How to use it?
  • Common Scanner methods
  • Difference between next() and nextLine()
  • Common mistakes
  • Interview questions

What is Scanner?

Scanner is a predefined class in Java used to read input from different sources such as:

  • Keyboard (System.in)
  • Files
  • Strings

It belongs to the java.util package, so we must import it before using it.

import java.util.Scanner;
Enter fullscreen mode Exit fullscreen mode

Why Do We Use Scanner?

Normally, a Java program prints fixed output.

System.out.println("Hello Java");
Enter fullscreen mode Exit fullscreen mode

But if you want the user to enter data while the program is running, you need the Scanner class.

For example:

Enter your name:
Enter fullscreen mode Exit fullscreen mode

The program waits for the user's input and processes it.


Creating a Scanner Object

Scanner sc = new Scanner(System.in);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Scanner → Class name
  • sc → Object name
  • new → Creates a new object
  • System.in → Reads input from the keyboard

Example: Reading a String

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.println("Welcome " + name);

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

Output

Enter your name:
Arul

Welcome Arul
Enter fullscreen mode Exit fullscreen mode

Common Scanner Methods

Method Description Example
next() Reads one word Java
nextLine() Reads an entire line Java Full Stack
nextInt() Reads an integer 25
nextDouble() Reads a decimal number 95.5
nextFloat() Reads a float 3.14
nextBoolean() Reads true or false true
nextLong() Reads a long value 9876543210

Reading an Integer

Scanner sc = new Scanner(System.in);

System.out.print("Enter your age: ");

int age = sc.nextInt();

System.out.println("Age is " + age);

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

Output

Enter your age:
22

Age is 22
Enter fullscreen mode Exit fullscreen mode

Reading Multiple Inputs

Scanner sc = new Scanner(System.in);

System.out.print("Enter your name: ");
String name = sc.nextLine();

System.out.print("Enter your age: ");
int age = sc.nextInt();

System.out.println(name);
System.out.println(age);

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

Difference Between next() and nextLine()

next()

Reads only one word.

Scanner sc = new Scanner(System.in);

String name = sc.next();
Enter fullscreen mode Exit fullscreen mode

Input

Java Programming
Enter fullscreen mode Exit fullscreen mode

Output

Java
Enter fullscreen mode Exit fullscreen mode

It stops reading when it encounters a space.


nextLine()

Reads the entire line.

Scanner sc = new Scanner(System.in);

String name = sc.nextLine();
Enter fullscreen mode Exit fullscreen mode

Input

Java Programming
Enter fullscreen mode Exit fullscreen mode

Output

Java Programming
Enter fullscreen mode Exit fullscreen mode

Common Mistake

Scanner sc = new Scanner(System.in);

System.out.print("Age: ");
int age = sc.nextInt();

System.out.print("Name: ");
String name = sc.nextLine();

System.out.println(name);
Enter fullscreen mode Exit fullscreen mode

Input

22
Arul
Enter fullscreen mode Exit fullscreen mode

The program prints an empty string for name.

Why?

nextInt() reads only the number and leaves the Enter key (\n) in the input buffer. The following nextLine() immediately reads that leftover newline.

Solution

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

String name = sc.nextLine();
Enter fullscreen mode Exit fullscreen mode

Why Do We Close Scanner?

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

Closing the Scanner releases the resources associated with it. It is considered a good programming practice to close it after use.


Interview Questions

1. What is Scanner?

Scanner is a predefined class in the java.util package used to read input from the keyboard, files, strings, and other input sources.

2. Why do we use Scanner?

We use Scanner to take input from the user while the program is running.

3. Which package contains Scanner?

java.util
Enter fullscreen mode Exit fullscreen mode

4. How do you create a Scanner object?

Scanner sc = new Scanner(System.in);
Enter fullscreen mode Exit fullscreen mode

5. What is the difference between next() and nextLine()?

next() nextLine()
Reads one word Reads the entire line
Stops at whitespace Reads until Enter is pressed

Conclusion

The Scanner class is one of the first Java classes every beginner should learn. It allows you to read user input easily and supports many data types such as int, double, String, and boolean.

Understanding methods like nextInt(), next(), and nextLine() will help you avoid common mistakes and write better Java programs.

Source: dev.to

arrow_back Back to Tutorials