Focusing specifically on console input and output in Java, let’s explore how to read various types of data from the console and utilize different printing options to display output effectively. These operations are crucial for interactive Java applications.
Reading Input from the Console
Java uses the Scanner
class from the java.util
package for reading console input. The Scanner
class provides methods to read different types of data, including int
, double
, String
, and more.
Reading a String
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class InputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); // Reads a line of input System.out.println("Your name is: " + name); scanner.close(); } } |
Reading an Integer
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class IntInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your age: "); int age = scanner.nextInt(); // Reads an integer System.out.println("Your age is: " + age); scanner.close(); } } |
Reading a Double
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class DoubleInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a decimal number: "); double number = scanner.nextDouble(); // Reads a double System.out.println("The number is: " + number); scanner.close(); } } |
Printing Options in Java
Java provides several methods for printing to the console, allowing for simple text output, formatted strings, and more.
Using System.out.println()
Prints the message to the console followed by a newline.
1 |
System.out.println("Hello, Java!"); |
Using System.out.print()
Prints the message to the console without adding a newline at the end.
1 2 |
System.out.print("Hello"); System.out.print(", Java!"); |
Using System.out.printf()
Allows for formatted output, similar to the printf
function in C/C++.
1 2 3 |
String name = "Java"; int version = 17; System.out.printf("Welcome to %s %d%n", name, version); // %n is a platform-independent newline |
Reading and Printing Multiple Data Types Together
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Scanner; public class MultipleInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name, age, and salary: "); String name = scanner.next(); // Reads the next token as a string int age = scanner.nextInt(); // Reads an integer double salary = scanner.nextDouble(); // Reads a double System.out.printf("Name: %s, Age: %d, Salary: %.2f%n", name, age, salary); scanner.close(); } } |
These examples showcase the flexibility of the Scanner
class for reading various data types from the console and the versatility of Java’s print methods for outputting text. Understanding these basic I/O operations is essential for building interactive Java applications.