Console Input and Output Operations
Displaying Output to Console
Basic Syntax:
System.out.println(message); // Output with newline
System.out.print(message); // Output without newline
System.out.printf(format, message); // Formatted output
printlnautomatically apppends\nwhileprintdoes notprintffollows similar formatting rules as C language printf
Code Examples:
System.out.println("Hello World");
int value = 42;
System.out.printf("%d", value);
Format Specifiers:
| Specifier | Data Type | Example | Output |
|---|---|---|---|
| d | Decimal integer | ("%d", 100) | 100 |
| x | Hexadecimal integer | ("%x", 100) | 64 |
| o | Octal integer | ("%o", 100) | 144 |
| f | Fixed-point float | ("%f", 100f) | 100.000000 |
| e | Exponential float | ("%e", 100f) | 1.000000e+02 |
| s | String | ("%s", 100) | 100 |
| c | Character | ("%c", 'A') | A |
| b | Boolean | ("%b", 100) | true |
Reading Input from Keyboard
Using Scanner for String/Integer/Float Input:
import java.util.Scanner;
Scanner inputReader = new Scanner(System.in);
System.out.println("Enter your name:");
String userName = inputReader.nextLine();
System.out.println("Enter your age:");
int userAge = inputReader.nextInt();
System.out.println("Enter your salary:");
float userSalary = inputReader.nextFloat();
System.out.println("User Details:");
System.out.println("Name: " + userName);
System.out.println("Age: " + userAge);
System.out.println("Salary: " + userSalary);
inputReader.close();
Reading Multiple Numbers and Calculating Average:
Scanner scanner = new Scanner(System.in);
double total = 0;
int count = 0;
while (scanner.hasNextDouble()) {
double number = scanner.nextDouble();
total += number;
count++;
}
System.out.println("Total: " + total);
System.out.println("Average: " + (total / count));
scanner.close();
Important Notes:
- Use Ctrl+Z (Windows) or Ctrl+D (Linux/Mac) to terminate input during multiple data entry
- Always close the Scanner object when done
Number Guessing Game Implementation
Game Rules
The system genreates a random number between 1 and 100. The player inputs guesses, receiving "Too low", "Too high", or "Correct guess" feedback until the correct number is found.
Complete Implementation
import java.util.Random;
import java.util.Scanner;
public class NumberGame {
public static void main(String[] args) {
Random generator = new Random();
Scanner input = new Scanner(System.in);
int targetNumber = generator.nextInt(100) + 1;
while (true) {
System.out.println("Guess a number (1-100):");
int userGuess = input.nextInt();
if (userGuess < targetNumber) {
System.out.println("Too low");
} else if (userGuess > targetNumber) {
System.out.println("Too high");
} else {
System.out.println("Correct! You guessed it.");
break;
}
}
input.close();
}
}
Implementation Breakdown
- Import Required Libraries: Random for number generation and Scanner for user input
- Class Definition: Main class containing the program entry point
- Initialization: Create Random and Scanner objects
- Target Generation: Generate random number between 1-100 using nextInt(100) + 1
- Game Loop: Continuous loop until correct guess
- Prompt user for number input
- Compare guess with target number
- Provide appropriate feedback
- Break loop when guess matches target
- Resource Cleanup: Close Scanner to release system resources