Chapter 11 of 57

User Input in Java

So far, our Java programs have used values that we directly wrote inside the code. For example:

int age = 25;
String name = "John";

But real applications need to get information from the user. For example, a program might ask for a person's name, age, marks, or choice.

In Java, one of the easiest ways to take input from the user is by using the Scanner class.

Using Scanner

First, we need to import Scanner:

import java.util.Scanner;

Then we create a Scanner object:

Scanner input = new Scanner(System.in);

Don't worry too much about objects yet. For now, just understand that input will allow us to read information entered by the user.

Let's see a complete example:

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

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

        System.out.println("Hello, " + name);
    }
}

If the user enters:

John

the output will be:

Enter your name: John
Hello, John

Here, nextLine() reads the text entered by the user and stores it in the name variable.

Taking Integer Input

If we want the user to enter a whole number, we can use nextInt().

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

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

        System.out.println("Your age is " + age);
    }
}

If the user enters 25, the value 25 is stored in the age variable.

So, nextInt() is used when we want to read an integer.

Taking Decimal Input

For decimal numbers, we can use nextDouble().

System.out.print("Enter your height: ");
double height = input.nextDouble();

System.out.println("Your height is " + height);

If the user enters:

5.9

then height will contain 5.9.

Similarly, there are methods for other common data types:

nextInt()      → int
nextDouble()   → double
nextFloat()    → float
nextLong()     → long
nextBoolean()  → boolean
nextLine()     → String

You don't need to memorize all of them immediately. As you practice, you'll naturally remember the ones you use most often.

Taking a Character Input

There is no direct nextChar() method in Scanner. If we want to read a single character, we can read a String and then take its first character.

For example:

System.out.print("Enter your grade: ");
char grade = input.next().charAt(0);

System.out.println("Your grade is " + grade);

If the user enters:

A

the value of grade will be 'A'.

Here, next() reads the entered text, and charAt(0) gets the first character.

next() vs nextLine()

One thing that often confuses beginners is the difference between next() and nextLine().

next() reads only one word:

System.out.print("Enter your name: ");
String name = input.next();

If the user enters:

John Smith

next() will read only John.

On the other hand, nextLine() reads the entire line:

String name = input.nextLine();

So if the user enters:

John Smith

nextLine() reads the complete John Smith.

For names or sentences that can contain spaces, nextLine() is usually the better choice.

Taking Multiple Inputs

We can take several inputs from the user in the same program.

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

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

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

        System.out.print("Enter your height: ");
        double height = input.nextDouble();

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Height: " + height);
    }
}

If the user enters:

John
25
5.9

the program will display:

Name: John
Age: 25
Height: 5.9

This is how programs start becoming interactive. Instead of using fixed values written by the programmer, the user can provide the information while the program is running.

One Common Scanner Issue

There is one small issue you should know about when using nextInt() or nextDouble() followed by nextLine().

For example:

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

Sometimes nextLine() may read the leftover newline instead of waiting for the user to enter text.

A common solution is to call nextLine() once after reading the number:

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

String name = input.nextLine();

You don't need to worry too much about this right now. Once you start using Scanner regularly, this behavior will become easier to understand.

Closing the Scanner

When we are finished taking input, we can close the Scanner:

input.close();

For example:

Scanner input = new Scanner(System.in);

// Take input here

input.close();

Closing the scanner is a good practice when you are finished using it.

A Simple Real-World Example

Let's create a small program that asks for a user's name and age.

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

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

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

        System.out.println("Hello, " + name + "!");
        System.out.println("You are " + age + " years old.");

        input.close();
    }
}

If John enters 25, the program can respond with:

Enter your name: John
Enter your age: 25
Hello, John!
You are 25 years old.

This simple concept of taking input is extremely important because almost every useful application needs some form of user-provided data.