Java User Input

Learn how to take user input in Java using Scanner class, different input methods, examples, common mistakes, and best practices.

What is User Input in Java?

User input allows users to enter values during program execution. Java provides the Scanner class to take input from keyboard.

Scanner class belongs to the java.util package.

Import Scanner Class

Before using Scanner, you must import it.

import java.util.Scanner;

Create Scanner Object

A Scanner object is required to read input.

Scanner sc = new Scanner(System.in);

Taking String Input

Use nextLine() method to take text input.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name:");

        String name = sc.nextLine();

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

    }

}

Output

Enter your name:
Alex

Welcome Alex

Taking Integer Input

Use nextInt() method to take integer input.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter age:");

        int age = sc.nextInt();

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

    }

}

Taking Double Input

Use nextDouble() method for decimal numbers.

double salary = sc.nextDouble();

Taking Boolean Input

Use nextBoolean() method for true/false values.

boolean isJavaEasy = sc.nextBoolean();

Taking Character Input

Scanner does not have direct method for char input. We use charAt() method.

char grade = sc.next().charAt(0);

Important Scanner Methods

Method Description
nextLine() Reads complete line
next() Reads single word
nextInt() Reads integer value
nextDouble() Reads decimal number
nextBoolean() Reads boolean value

Difference Between next() and nextLine()

Method Description Example Input Output
next() Reads only one word Hello Java Hello
nextLine() Reads complete line Hello Java Hello Java

Taking Multiple Inputs

import java.util.Scanner;

public class Student {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

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

        System.out.println("Enter marks:");
        double marks = sc.nextDouble();

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

    }

}

Common Scanner Issue

Sometimes nextLine() gets skipped after nextInt(). This happens because nextInt() leaves newline character in buffer.

int age = sc.nextInt();

sc.nextLine();

String name = sc.nextLine();

Adding extra nextLine() clears the buffer.

Closing Scanner

It is good practice to close Scanner object after use.

sc.close();

Complete Java User Input Program

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

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

        System.out.println("Enter your marks:");
        double marks = sc.nextDouble();

        System.out.println("Name: " + name);

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

        System.out.println("Marks: " + marks);

        sc.close();

    }

}

Output

Enter your name:
Alex

Enter your age:
20

Enter your marks:
89.5

Name: Alex
Age: 20
Marks: 89.5

Best Practices

Common Errors

Conclusion

User input is an important concept in Java programming. Scanner class makes it easy to read values from keyboard and build interactive applications.