Java Lesson — Scanner, stdin, stdout, and stderr

Learn how Java reads input, prints normal output, and prints error output.

What this lesson covers

In this lesson, you will learn how a Java program reads data from the user using Scanner, how it prints normal output using System.out, and how it prints error messages using System.err. You will also learn the idea of stdin, stdout, and stderr.

These ideas are very important because almost every console Java program uses them. If you understand this lesson well, you will be able to make interactive Java programs that ask the user for values and then respond with useful output.

stdin

Standard input, usually the keyboard

stdout

Standard output, normal program output

stderr

Standard error, error messages

Scanner

Java class used to read input

What are stdin, stdout, and stderr?

Many programming languages use three standard streams for communication. Java also uses them.

Name Full form Meaning Java form
stdin Standard input Data coming into the program, usually from keyboard System.in
stdout Standard output Normal output shown by the program System.out
stderr Standard error Error output shown separately from normal output System.err

In simple classroom programs, stdout and stderr may both appear on the same console, but they are conceptually different streams.

Using Scanner with System.in

To read input from the keyboard in Java, we usually use the Scanner class. The scanner reads from System.in, which represents standard input.

Import Scanner

import java.util.Scanner;

Basic example

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

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

        sc.close();
    }
}

In this example, the program asks for a name, reads the full line using nextLine(), and then prints a greeting.

Common Scanner methods

Scanner provides different methods for different kinds of input.

Method Reads Example input
next() One word Java
nextLine() Full line Hello Java World
nextInt() Integer 25
nextDouble() Decimal number 78.5
nextBoolean() Boolean value true

Reading numbers

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int a = sc.nextInt();

        System.out.print("Enter second number: ");
        int b = sc.nextInt();

        int sum = a + b;

        System.out.println("Sum = " + sum);

        sc.close();
    }
}

stdout with System.out

System.out is used for normal output. It sends output to the standard output stream. Two very common methods are print() and println().

System.out.print("Hello ");
System.out.println("World");

print() keeps the cursor on the same line. println() prints and then moves to the next line.

Formatted output

String name = "Aman";
int marks = 92;

System.out.printf("Student: %s, Marks: %d%n", name, marks);

printf() is useful when you want cleaner formatted output.

stderr with System.err

System.err is used for error messages. It sends output to the standard error stream. This is helpful when you want to separate normal program output from warnings or errors.

System.err.println("Invalid input. Please enter a positive number.");

In some IDEs and terminals, error output may appear in a different color or may be handled separately.

Example using stdout and stderr together

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        if (age < 0) {
            System.err.println("Error: age cannot be negative.");
        } else {
            System.out.println("Your age is " + age);
        }

        sc.close();
    }
}

Important note about nextInt() and nextLine()

One common beginner problem happens when we use nextInt() and then immediately use nextLine(). After reading the integer, the newline character is still left in the input buffer.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        sc.nextLine(); // consume leftover newline

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

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

        sc.close();
    }
}

That extra sc.nextLine() is used to consume the leftover newline before reading the actual text line.

Complete example

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("=== Student Information Form ===");

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

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

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

        if (age < 0) {
            System.err.println("Error: age cannot be negative.");
        } else {
            System.out.println();
            System.out.println("=== Output ===");
            System.out.println("Name  : " + name);
            System.out.println("Age   : " + age);
            System.out.println("Marks : " + marks);
        }

        sc.close();
    }
}

Assignments

Assignment 1: Write a program that reads a student's name and prints Hello, <name>.
Assignment 2: Write a program that reads two integers using Scanner and prints their sum using System.out.println().
Assignment 3: Write a program that reads an age. If the age is negative, print an error message using System.err.println().
Assignment 4: Write a program that reads a full name using nextLine() and a city using nextLine(), then prints both values.
Assignment 5: Write a program that reads an integer and a decimal number and prints them using printf().
Assignment 6: Write a program that first reads an integer and then a full line of text. Handle the leftover newline correctly.

Interactive MCQ Quiz

Choose one answer for each question and click Check Answers.

Total Questions: 0

0 / 0

Your result will appear here.

Quick revision