Output: Displaying Information with System.out
In Java, the most common way to send information to the console is by using the System class.
System.out.println() vs System.out.print()
println(): Prints the text and then moves the cursor to a new line. Think of it like hitting “Enter” after typing.print(): Prints the text but keeps the cursor on the same line.
Syntax and Examples
System.out.println("Hello, University!"); // Prints and moves to next line
System.out.print("This is Java "); // Prints and stays on same line
System.out.print("Lesson 3."); // Appears right after the previous text
Output:
Hello, University!
This is Java Lesson 3.
2. Input: Getting Data with the Scanner Class
To take input from a user (like their name or age), Java provides the Scanner class, which is part of the java.util package.
Step-by-Step Implementation
To use Scanner, you must follow these three steps:
Import the Class: You need to tell Java where the Scanner tool is located.
Create a Scanner Object: This “activates” the scanner to listen to the keyboard.
Use Scanner Methods: Use specific commands depending on the type of data (numbers, words, etc.) you are expecting.
Common Scanner Methods
| Method | Use Case |
next() | Reads a single word (stops at space). |
nextLine() | Reads a whole line of text (including spaces). |
nextInt() | Reads an integer (whole number). |
nextDouble() | Reads a decimal number. |
3. Practical Example: A Student Profile Program
This program combines both Input and Output. It asks for a student’s name and GPA, then displays them back.
import java.util.Scanner; // Step 1: Import
public class StudentInfo {
public static void main(String[] args) {
// Step 2: Create Scanner object
Scanner input = new Scanner(System.in);
// Step 3: Use methods to get data
System.out.print("Enter your full name: ");
String name = input.nextLine();
System.out.print("Enter your GPA: ");
double gpa = input.nextDouble();
// Output the results
System.out.println("\n--- Student Profile ---");
System.out.println("Name: " + name);
System.out.println("Current GPA: " + gpa);
// Good practice: close the scanner
input.close();
}
}
4. Key Concepts for University Exams
If you are studying this for a university course, keep these technical points in mind:
The “System.in” Parameter
When we write new Scanner(System.in), the System.in represents the Standard Input Stream, which is usually the keyboard.
Data Type Mismatch
If you use nextInt() but the user types a word like “Hello,” the program will crash with an InputMismatchException. This is a common error university students face in lab tasks.
The nextLine() Issue
A common “trap” in Java is using nextLine() immediately after nextInt(). The nextInt() method leaves a “newline” character in the buffer, causing the nextLine() to appear as if it was skipped.
Fix: Add an extra
input.nextLine();to “clean” the buffer after reading numbers.
5. Summary and Best Practices
Always Import: Don’t forget
import java.util.Scanner;at the very top of your file.Prompt the User: Always use
System.out.print()before an input line so the user knows what they are supposed to type.Close the Scanner: While not always required in small assignments, using
input.close()is a professional habit that prevents “memory leaks.”