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.
Standard input, usually the keyboard
Standard output, normal program output
Standard error, error messages
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
Hello, <name>.
System.out.println().
System.err.println().
nextLine() and a city using nextLine(), then prints both values.
printf().
Interactive MCQ Quiz
Choose one answer for each question and click Check Answers.
0 / 0
Your result will appear here.
Quick revision
System.inis standard input.Scanneris commonly used to read input fromSystem.in.System.outis used for normal output.System.erris used for error messages.nextLine()reads a full line.nextInt()reads an integer.- After
nextInt(), you may need an extranextLine()before reading text.