codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

38 / 65 topics
38Java User Input39Java Date
Tutorials/Java Programming/Java User Input
☕Java Programming

Java User Input

Updated 2026-05-12
20 min read

Java User Input

In this tutorial, you'll learn how to read user input in Java using the Scanner class. This is a fundamental skill for creating interactive applications where you need to gather data from users. Understanding how to handle different types of input will make your programs more versatile and responsive.

Introduction

User input is essential for any application that interacts with humans. In Java, the Scanner class provides a convenient way to read input from the user through the console. This tutorial will cover how to use the Scanner class to read various types of data, including strings, integers, and more.

Core Content

Understanding the Scanner Class

The Scanner class is part of the java.util package and allows you to read different types of input from the user. It can be used to read from standard input (keyboard), files, or other input streams.

Importing the Scanner Class

To use the Scanner class, you need to import it at the beginning of your Java program:

Java
1

Reading Strings with nextLine()

The nextLine() method reads a line of text from the user. It is useful for reading strings that may contain spaces.

Example: Reading a String

ReadString.java
1import java.util.Scanner;
2
3public class ReadString {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6
7 System.out.print("Enter your name: ");
8 String name = scanner.nextLine();
9
10 System.out.println("Hello, " + name + "!");
11
12 scanner.close();
13 }
14}
Output
Enter your name: John Doe
Hello, John Doe!
Terminal
$ javac ReadString.java
$ java ReadString

In this example, the program prompts the user to enter their name and then greets them with a personalized message.

Reading Integers with nextInt()

The nextInt() method reads an integer value from the user. It is useful for reading numeric input that you want to perform calculations on.

Example: Reading an Integer

ReadInteger.java
1import java.util.Scanner;
2
3public class ReadInteger {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6
7 System.out.print("Enter your age: ");
8 int age = scanner.nextInt();
9
10 System.out.println("You are " + age + " years old.");
11
12 scanner.close();
13 }
14}
Output
Enter your age: 30
You are 30 years old.
Terminal
$ javac ReadInteger.java
$ java ReadInteger

In this example, the program prompts the user to enter their age and then displays it back.

Reading Other Types of Input

The Scanner class provides methods for reading other types of data as well, such as:

  • nextDouble() for double values
  • nextFloat() for float values
  • nextBoolean() for boolean values

Example: Reading Multiple Types

ReadMultipleTypes.java
1import java.util.Scanner;
2
3public class ReadMultipleTypes {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6
7 System.out.print("Enter your name: ");
8 String name = scanner.nextLine();
9
10 System.out.print("Enter your age: ");
11 int age = scanner.nextInt();
12
13 System.out.print("Enter your height in meters: ");
14 double height = scanner.nextDouble();
15
16 System.out.println("Name: " + name);
17 System.out.println("Age: " + age);
18 System.out.println("Height: " + height + " meters");
19
20 scanner.close();
21 }
22}
Output
Enter your name: Alice
Enter your age: 25
Enter your height in meters: 1.65
Name: Alice
Age: 25
Height: 1.65 meters
Terminal
$ javac ReadMultipleTypes.java
$ java ReadMultipleTypes

In this example, the program reads a string, an integer, and a double from the user and then displays them.

Common Mistakes

  • Not Closing the Scanner: Always remember to close the Scanner object using scanner.close() to free up system resources.
Java
1scanner.close();
  • Handling Input Errors: If the user enters data that doesn't match the expected type, it can cause a InputMismatchException. You should handle such exceptions using try-catch blocks.
Java
1try {
2 int age = scanner.nextInt();
3} catch (InputMismatchException e) {
4 System.out.println("Please enter a valid integer.");
5}

Practical Example

Let's create a practical example that combines reading different types of input to calculate the BMI (Body Mass Index) of a user.

BMI.java
1import java.util.Scanner;
2
3public class BMI {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6
7 System.out.print("Enter your name: ");
8 String name = scanner.nextLine();
9
10 System.out.print("Enter your weight in kilograms: ");
11 double weight = scanner.nextDouble();
12
13 System.out.print("Enter your height in meters: ");
14 double height = scanner.nextDouble();
15
16 double bmi = weight / (height * height);
17
18 System.out.println(name + ", your BMI is " + bmi);
19
20 scanner.close();
21 }
22}
Output
Enter your name: Bob
Enter your weight in kilograms: 70
Enter your height in meters: 1.75
Bob, your BMI is 22.857142857142858
Terminal
$ javac BMI.java
$ java BMI

In this example, the program reads a user's name, weight, and height, calculates their BMI, and then displays it.

Summary

  • The Scanner class is used to read input from the user.
  • Use nextLine() for reading strings.
  • Use nextInt(), nextDouble(), etc., for reading different numeric types.
  • Always close the Scanner object after use.
  • Handle input errors using try-catch blocks.

What's Next?

In the next tutorial, we will explore how to work with dates in Java. Understanding date and time manipulation is crucial for many applications, such as scheduling systems or logging tools. Stay tuned!


PreviousJava EnumNext Java Date

Recommended Gear

Java EnumJava Date