Java User Input


User input is one of the most important parts of any program. In Java, we can take input from users in several ways such as using the Scanner class, BufferedReader, or even Command-Line Arguments. Among these, the Scanner class is the most common and easiest to use for beginners. In this tutorial, you’ll learn how to take input from users, handle different data types, and understand how Java reads data from the console.

What is User Input in Java?

User input means allowing a user to provide information while the program is running. For example, when a program asks for your name or age, it waits for you to type a value and press Enter. That data can then be used within the program.

Java provides the java.util.Scanner class to make reading user input simple. Before using it, you need to import the Scanner class from the Java utility package.

import java.util.Scanner;

After importing, you can create a Scanner object to start taking input from the keyboard.

How to Use the Scanner Class

The Scanner class is used to read input from different sources such as the keyboard, files, or streams. When you read input from the keyboard, you use:

Scanner input = new Scanner(System.in);

Here:

  • System.in represents standard input (keyboard).

  • input is the Scanner object used to read data.

  • You can then use various Scanner methods to read values of different types.

Reading Different Types of Input

The Scanner class has specific methods for each type of data. Let’s look at the most common ones.

Reading a String

import java.util.Scanner;

class Example {
    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 + "!");
    }
}
  • nextLine() reads the entire line entered by the user, including spaces.

  • If you use next() instead, it will only read one word (until the first space).

Reading an Integer

System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old.");

The method nextInt() reads integer values. If the user types a non-numeric value, it will throw an error, so you need to ensure valid input.

Reading a Float or Double

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

The methods nextFloat() and nextDouble() read decimal numbers.

Reading a Character

The Scanner class doesn’t have a direct method to read a single character, but you can use this trick:

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

The charAt(0) picks the first character of the user input.

Handling Multiple Inputs

You can use the same Scanner object to take multiple inputs from the user.

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 salary: ");
double salary = input.nextDouble();

System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);

Each call to a Scanner method reads the next available input from the user.

Common Scanner Methods

Method Description
next() Reads a single word
nextLine() Reads a full line (including spaces)
nextInt() Reads an integer
nextDouble() Reads a double value
nextFloat() Reads a float value
nextBoolean() Reads a boolean value (true/false)
nextLong() Reads a long value
close() Closes the Scanner object

Closing the Scanner

When you finish reading user input, always close the Scanner to free system resources.

input.close();

However, you should close the Scanner only at the end of the program because closing it also closes System.in, which cannot be reopened later.

Taking Input with BufferedReader (Alternative Way)

Before the Scanner class, Java developers often used BufferedReader for reading input.

import java.io.*;

class InputExample {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your city: ");
        String city = reader.readLine();
        System.out.println("You live in " + city);
    }
}

The BufferedReader is faster but less convenient because it throws checked exceptions and reads only strings.

Summary of the Tutorial

In this tutorial, you learned how to take input from users in Java using the Scanner class and BufferedReader. The Scanner class is simple, supports multiple data types, and is ideal for most beginner-level programs. Remember to close your Scanner at the end and handle possible input mismatches using proper validation.


Practice Questions

  1. Write a Java program that asks the user for their name and age, then displays a message saying “Hello [name], you are [age] years old.”

  2. Create a program that takes three numbers as input from the user and displays their sum, average, and product.

  3. Write a Java program to take a student’s name, marks in three subjects, and calculate the total and average marks.

  4. Build a program that takes a user’s height and weight as input and calculates their Body Mass Index (BMI).

  5. Create a program that asks for two integers and prints which one is greater using user input.

  6. Write a Java program that reads a full sentence from the user using nextLine() and displays the total number of characters in it.Would you like me to start the next tutorial — Would you like me to start the next tutorial — Java Date?Java Date?

  7. Take two double numbers from the user and perform addition, subtraction, multiplication, and division operations.

  8. Write a program that asks for a user’s favorite color and prints a message like “Your favorite color is Blue.”

  9. Create a program that takes a character input from the user and displays its ASCII value.

  10. Would you like me to start the next tutorial — Java Date?Write a Java program that asks the user for their age and prints “You are eligible to vote” if age is 18 or above, otherwise prints “You are not eligible to vote.”


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top