On this page
Input and Output in Java
Java provides several classes and methods to perform input and output (I/O) operations. In this section, we will cover basic I/O operations using the Scanner
class for input and the System.out
class for output.
Output in Java
Java provides the System.out
class to perform output operations. The System.out.println
method is commonly used to print text to the console.
Example
public class OutputExample {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Java programming is fun.");
}
}
Explanation
System.out.println("Hello, World!");
prints “Hello, World!” followed by a new line.System.out.println("Java programming is fun.");
prints “Java programming is fun.” followed by a new line.
Input in Java
To read input from the user, we can use the Scanner
class from the java.util package. The Scanner
class provides various methods to read different types of input.
Example
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();
System.out.println("Hello, " + name + "!");
}
}
Explanation
import java.util.Scanner;
imports the Scanner class.Scanner scanner = new Scanner(System.in);
creates a Scanner object to read input from the standard input stream (keyboard).String name = scanner.nextLine();
reads a line of text entered by the user.System.out.println("Hello, " + name + "!");
prints a greeting message that includes the user’s name.
Reading Different Types of Input
The Scanner
class provides methods to read various types of input, such as integers, doubles, and booleans.
Example
import java.util.Scanner;
public class MultipleInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int intValue = scanner.nextInt();
System.out.print("Enter a double: ");
double doubleValue = scanner.nextDouble();
System.out.print("Enter a boolean: ");
boolean booleanValue = scanner.nextBoolean();
System.out.println("Integer: " + intValue);
System.out.println("Double: " + doubleValue);
System.out.println("Boolean: " + booleanValue);
}
}
Explanation
int intValue = scanner.nextInt();
reads an integer input.double doubleValue = scanner.nextDouble();
reads a double input.boolean booleanValue = scanner.nextBoolean();
reads a boolean input.
Example Program
Here is a complete example program that combines input and output operations:
import java.util.Scanner;
public class UserInfo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your height in meters: ");
double height = scanner.nextDouble();
System.out.println("\nUser Information:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height + " meters");
}
}
Explanation
- The program reads the user’s name, age, and height.
- The program then prints the collected information.