Java Output
We use System.out.println() to print text to the console.
System.out.println("Hello World"); // Prints on new line
System.out.print("Hello"); // Prints on same line
Java Input (Scanner Class)
The Scanner class is used to get user input.
import java.util.Scanner;public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter age: ");
int age = scanner.nextInt();
System.out.println("Hello " + name + ", you are " + age);
scanner.close(); // Good practice to close scanner
}
}
Scanner Methods
nextBoolean()- Reads a booleannextByte()- Reads a bytenextDouble()- Reads a doublenextFloat()- Reads a floatnextInt()- Reads an intnextLine()- Reads a String (line)nextLong()- Reads a longnextShort()- Reads a short