What You Will Learn
- How to print output using System.out.print() and System.out.println()
- How to read user input using the Scanner class
- How to format output using printf() and format()
1. Printing Output in Java
Output in Java means showing information to the user. Most Java programs interact with users by displaying messages, results, or instructions on the screen using output statements.
The System.out object represents the standard output stream, which usually means the console or terminal window. It is commonly used while learning Java, debugging programs, and building command-line applications.
Real-life example: When you use an ATM machine, messages like "Enter your PIN" or "Transaction Successful" are displayed using output statements behind the scenes.
Java provides two main methods for displaying output:
System.out.println()– prints text and moves to a new lineSystem.out.print()– prints text without a new line
Try this example:
public class OutputDemo {
public static void main(String[] args) {
System.out.println("Welcome to VINAR TECH Java Tutorial!");
System.out.print("This is printed on the same line. ");
System.out.println("This appears after print().");
}
}2. Taking User Input using Scanner
Input in Java means receiving data from the user while the program is running. Instead of hardcoding values inside the program, Java allows users to enter information such as names, numbers, or choices at runtime.
The Scanner class is the most beginner-friendly way to take input in Java. It reads data from the keyboard and converts it into different data types like integer, string, or boolean.
Real-life example: While filling an online form, you enter details such as your name, age, and phone number. A Java program reads this information using input methods before processing or saving it.
The Scanner class is used to accept input from the user. It belongs to the java.util package.
Try example:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Hello " + name + ", your age is " + age + ".");
scanner.close();
}
}Common Scanner Methods
Each Scanner method is designed for a specific type of input. Choosing the correct method avoids runtime errors and ensures that data is read accurately.
Important tip: When using nextInt() followed by nextLine(), always handle the newline properly to avoid skipping input. This is a very common beginner mistake in Java.
nextLine()– reads a full line of textnextInt()– reads an integernextFloat()– reads a floating-point numbernextBoolean()– reads true/false
3. Formatting Output
Formatted output helps present information in a clean and professional way. Instead of printing raw values, Java allows developers to control decimal places, alignment, and text layout using format specifiers.
The printf() method is widely used in real-world applications such as invoices, reports, logs, and financial summaries. It makes output easier to read and understand.
Real-life example: An e-commerce invoice shows item prices with exactly two decimal places and neatly aligned columns. This formatting is handled using formatted output techniques in Java.
Use System.out.printf() or String.format() to create formatted output.
Try example:
public class FormatDemo {
public static void main(String[] args) {
String product = "Laptop";
int quantity = 3;
double price = 49999.50;
System.out.printf("Item: %s | Qty: %d | Price: %.2f%n", product, quantity, price);
}
}Common Format Specifiers
%s– String%d– Integer%f– Floating numbers%.2f– Floating value with 2 decimal places%n– New line
4. Common Input & Output Mistakes (Beginner Tips)
- Forgetting to import
java.util.Scanner - Using the wrong Scanner method for the expected data type
- Not closing the Scanner after use
- Mixing
nextLine()with numeric input incorrectly - Assuming user input will always be valid
Always validate user input and handle unexpected values. Good input handling makes your program more reliable and user-friendly.
Difference Between print(), println(), and printf()
Java provides multiple ways to display output, and each method is useful in different situations. Understanding when to use each one helps you write clearer and more readable programs.
- print() displays text without moving the cursor to a new line. It is useful when you want multiple outputs on the same line.
- println() displays text and automatically moves to the next line. This is the most commonly used method for beginners.
- printf() displays formatted output and gives you full control over how numbers and text appear.
Real-life example:
A train ticket printout uses simple messages (println), while a billing invoice uses formatted output (printf) to align prices and totals neatly.
Why Input & Output Is Important in Real Applications
Input and Output form the foundation of user interaction in any software system. Without I/O, a program cannot receive instructions from users or display meaningful results.
Almost every real-world Java application depends heavily on input and output. Whether it is a banking system, a student management portal, or a simple command-line utility, I/O is always involved.
Real-life example:
When you log in to a website, the system takes your username and password as input, validates the data, and then displays either a success or error message as output. All of this logic is handled using input and output operations internally.
As you move forward in Java, the same I/O concepts extend to file handling, network communication, APIs, and database operations. Mastering basic input and output now makes advanced topics much easier later.