๐จ๏ธ Input/Output Statements in Java
Java provides various mechanisms to accept input from the user and display output to the screen, typically using classes from the java.io
and java.util
packages.
๐ฅ Input in Java
The most common way to accept user input is through the Scanner
class.
๐น Example Using Scanner
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}
๐ค Output in Java
Java uses System.out.println()
and System.out.print()
for output.
System.out.println()
โ prints with a newlineSystem.out.print()
โ prints without a newline
๐น Example
System.out.println("Hello, world!"); // newline
System.out.print("Hello, "); // no newline
System.out.print("Java!"); // continues on same line
๐ Other I/O Options
BufferedReader
(legacy, more control)Console
(secure password entry)FileReader
,FileWriter
(for file I/O)