Display console output
System.out.print() remains on the same line. System.out.println() prints and then starts a new line.
Input is data a program reads (keyboard, file, network); output is data it writes (console, file, socket). Java models both as streams: a sequence of bytes or characters flowing in one direction.
InputStream, OutputStream) handle raw bytes: images, PDFs, network data.Reader, Writer) handle text and take care of character encoding (UTF-8).System.in is the keyboard input stream, System.out the console output stream, System.err the error stream.import java.util.Scanner;
public class Greeting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine(); // whole line
System.out.print("Enter your age: ");
int age = sc.nextInt(); // next integer token
System.out.printf("Hello %s, next year you will be %d%n", name, age + 1);
sc.close();
}
}
Note: after nextInt() the newline is still in the buffer, so a following nextLine() returns an empty string. Call sc.nextLine() once to consume it.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken());
BufferedReader reads large chunks at once and is many times faster than Scanner for big inputs.
System.out.println("Total: " + 42); // prints and adds a newline
System.out.print("No newline");
System.out.printf("%-10s|%8.2f%n", "Laptop", 54999.5); // formatted: left-aligned text, 2 decimals
Path path = Path.of("jobs.txt");
Files.writeString(path, "Java Developer\nSpring Boot Engineer\n");
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
try (BufferedWriter w = Files.newBufferedWriter(Path.of("out.csv"))) {
w.write("id,title");
w.newLine();
}
%d integer, %s string, %.2f decimal, %n newline.Files and try-with-resources for files so streams are always closed.System.out.print() remains on the same line. System.out.println() prints and then starts a new line.
Import java.util.Scanner, connect it to System.in, then use the method matching the value type.
nextInt(): integer · nextLong(): long · nextDouble(): decimal · next(): word · nextLine(): line · next().charAt(0): character.
After nextInt() or nextDouble(), consume the remaining newline before nextLine(), or it may appear to be skipped.
Select Run to test the supplied input.
This guided browser lab validates this lesson's input/output flow without executing untrusted code on the Laravel server.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
double height = scanner.nextDouble();
char grade = scanner.next().charAt(0);
System.out.println("----- Student Details -----");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
scanner.close();
}
}
Ask for a full name, city, age and favourite programming language, then print a formatted profile summary.
Which statement is correct?