




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


Java Basic Input and Output - Scanner, System.in, System.out
Basic Input and Output in Java
When you first write a Java program, it’s natural to want interaction — a two-way conversation between the user and the machine. Java enables this through standard input and output streams.
This tutorial will walk you through how to accept user input and print output using beginner-friendly and practical approaches.
Java Output – Writing to Console
Let’s begin with the most common way to display information in Java: System.out.print()
and System.out.println()
.
Using System.out.print() and println()
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World");
System.out.println("!");
}
}
Hello World!
Explanation:
System.out.print()
prints without moving to the next line, whereas System.out.println()
adds a new line after printing. It's essential to use the right one depending on whether you want continued or separate output lines.
Java Input – Reading from User
To accept input from the keyboard, Java commonly uses the Scanner
class from the java.util
package.
Example: Reading User Input
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Enter your name: Alice
Hello, Alice!
Explanation:
input.nextLine()
reads a full line of text. Scanner provides different methods for different types of input:
nextInt()
– for integersnextDouble()
– for floating-point numbersnext()
– for single word (stops at whitespace)
Reading Different Types of Input
Example: Reading Numbers
import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = input.nextInt();
System.out.print("Enter second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Enter first number: 10
Enter second number: 20
Sum: 30
Checks and Validations
While using nextInt()
, if the user inputs something non-numeric, your program will throw an InputMismatchException
. To handle such cases, use input validation:
if (input.hasNextInt()) {
int value = input.nextInt();
} else {
System.out.println("Invalid input. Please enter a number.");
}
Common Input Mistakes and How to Avoid Them
Problem: nextLine() after nextInt()
If you use nextLine()
after nextInt()
, you might skip input. This happens because nextInt()
doesn’t consume the newline character. Use an extra nextLine()
to clear the buffer:
input.nextLine(); // consume leftover newline
Key Takeaways
- Use
System.out.print()
andSystem.out.println()
for output - Use
Scanner
for reading user input - Match method to input type:
nextLine()
,nextInt()
,nextDouble()
, etc. - Validate input to avoid runtime errors
- Handle the newline issue when switching input types
QUIZ
Question 4:What will be the output of the following code?
System.out.print("Java ");
System.out.print("is ");
System.out.println("fun!");
System.out.print("Java ");
System.out.print("is ");
System.out.println("fun!");
Comments
Loading comments...