Java Lesson 9 - Input & Output | Dataplexa

Input and Output in Java

So far, our Java programs have displayed fixed output. But real programs become useful only when they can interact with users.

Input and output allow Java programs to receive data from users, process that data, and display meaningful results. This lesson focuses on how Java communicates with the outside world.


What Is Input and Output?

Input means receiving data from a user or another source. Output means displaying data back to the user.

In real-world applications, input may come from keyboards, files, databases, or network requests, while output may appear on screens, reports, or logs.

For beginners, we start with keyboard input and console output.


Output Using System.out.println()

You have already used System.out.println() in previous lessons. It is the most common way to display output in Java.

It prints text or values to the console and moves the cursor to the next line.

public class OutputExample {

    public static void main(String[] args) {
        System.out.println("Welcome to Java");
        System.out.println("Learning input and output");
    }
}

This method is mainly used for displaying messages, debugging, and showing results to users.


Why We Need User Input

Programs that only print fixed values are limited. To make programs dynamic, Java must accept input from users.

For example:

  • Entering a name during registration
  • Typing marks in a student application
  • Providing values for calculations

Java provides the Scanner class to read input easily.


Taking Input Using Scanner

The Scanner class reads data from the keyboard. It belongs to the java.util package.

Before using it, we must import the package and create a Scanner object.

import java.util.Scanner;

public class InputExample {

    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("Hello " + name);
        System.out.println("Your age is " + age);
    }
}

Here, Java waits for the user to type input and press Enter. The entered data is then stored in variables.


Different Types of Input Methods

The Scanner class provides different methods for different data types.

  • nextInt() – reads integer values
  • nextDouble() – reads decimal values
  • nextLine() – reads text (strings)
  • nextBoolean() – reads true or false

Choosing the correct method is important to avoid runtime errors.


Real-World Example

Imagine a simple billing application. The program asks for quantity and price, then calculates the total.

import java.util.Scanner;

public class BillingExample {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

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

        double total = quantity * price;

        System.out.println("Total amount: " + total);
    }
}

This example shows how input and output work together to build meaningful programs.


Common Mistakes to Avoid

Beginners often face issues while working with input. Some common mistakes include:

  • Forgetting to import the Scanner package
  • Using the wrong input method
  • Mixing nextLine() with numeric inputs incorrectly

These issues will be discussed in more detail in upcoming lessons.


What You Learned in This Lesson

  • What input and output mean in Java
  • How to display output using System.out
  • How to take user input using Scanner
  • How input and output work in real programs

In the next lesson, you will learn about control statements, which allow Java programs to make decisions based on input.