Basic Input Output
Basic Input Output
A school program needs reliable data and clear rules. Input supplies values while a program is running; output reports its results. Scanner can read a number from a text source using the same nextInt operation used with keyboard input.
Understand
Input supplies values while a program is running; output reports its results.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
nextInt leaves the line ending behind when reading keyboard text; an immediate nextLine may read the rest of that same line. Validate input before using it.
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.println("Enter your name: ");
String name = input.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Enter your name:
Ram
Hello, Ram!
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.println("Enter first number: ");
int num1 = input.nextInt();
System.out.println("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
Java · example discussed here
input.nextLine(); // consume leftover newline
- Use
System.out.print()andSystem.out.println()for output - Use
Scannerfor 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
Important boundaries and mistakes
nextInt leaves the line ending behind when reading keyboard text; an immediate nextLine may read the rest of that same line. Validate input before using it.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: nextInt leaves the line ending behind when reading keyboard text; an immediate nextLine may read the rest of that same line.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner("72");
int marks = input.nextInt();
System.out.println(marks);
input.close();
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner("72");
int marks = input.nextInt();
System.out.println(marks);
input.close();
}
}
Expected output
72Input supplies values while a program is running; output reports its results. Scanner can read a number from a text source using the same nextInt operation used with keyboard input.
nextInt leaves the line ending behind when reading keyboard text; an immediate nextLine may read the rest of that same line. Validate input before using it.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Check your understanding
What to remember
Input supplies values while a program is running; output reports its results. Scanner can read a number from a text source using the same nextInt operation used with keyboard input. nextInt leaves the line ending behind when reading keyboard text; an immediate nextLine may read the rest of that same line. Validate input before using it.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.