A running JVM has three byte streams already open:
| Stream | Typical use |
|---|---|
System.out | Normal program output (a PrintStream) |
System.err | Errors and diagnostics — stays visible when out is redirected |
System.in | Keyboard / piped input (an InputStream) |
println on out is fine for learning and CLIs. It is not structured logging (see Professional practice).out. Tests and shells redirect them separately.System.out is a shared global. In libraries, accept an Appendable or logger instead of printing.Mental model: output is a stream of characters, not a “return value.” println does not give a value back to the caller of main. Input is tokens or lines pulled from a stream that may block.
System.out.print("a");
System.out.println("b"); // a then b then newline
System.out.printf("%s %04d%n", "id", 7); // id 0007
| Conversion | Meaning |
|---|---|
%s | String (any object via toString) |
%d | Integer (byte/short/int/long) |
%f | Floating decimal |
%n | Platform newline (prefer over \n in format strings) |
%% | A literal % |
println(array) does not print elements — use Arrays.toString.IllegalFormatConversionException at run time, not compile time.printf("%d", 3.5) fails. %f with an int fails. Match the type.Scanner (java.util) splits input into tokens using whitespace by default, or reads whole lines.
Scanner in = new Scanner(System.in); int n = in.nextInt(); String word = in.next(); String line = in.nextLine();
| Method | Reads | Stops at |
|---|---|---|
next() | Next token | Whitespace |
nextInt / nextDouble / nextBoolean | A token, parsed | Whitespace |
nextLine() | Rest of the current line (may be empty) | Newline (consumed) |
hasNextInt() (and friends) | — | True if the next token parses |
nextInt() does not consume the leftover newline. A following nextLine() often returns the empty remainder of that line.nextInt → InputMismatchException. The bad token stays in the scanner.NoSuchElementException if you call next* without hasNext*.// Incorrect — name is usually empty int age = in.nextInt(); String name = in.nextLine(); // Correct — eat the leftover newline int age = in.nextInt(); in.nextLine(); String name = in.nextLine(); // Also correct — everything as lines, then parse int age = Integer.parseInt(in.nextLine().trim()); String name = in.nextLine();
Prefer reading lines and parsing when the input is interactive forms (age then a full name with spaces). Use token methods when the file is a stream of numbers.
if (!in.hasNextInt()) {
System.err.println("expected an integer");
in.next(); // skip garbage, or exit
} else {
int n = in.nextInt();
}
Integer.parseInt throws NumberFormatException on junk. Catch at the UI boundary; do not sprinkle it through domain code.trim() before parse. Users type spaces.age < 0). Parsing success is not domain success.Scanner uses the default locale for nextDouble (comma vs dot). For files, set Locale.ROOT or parse yourself.public static void main(String[] args) {
if (args.length < 1) {
System.err.println("usage: java App <file>");
return;
}
String file = args[0];
}
args is never null. It may have length 0.Scanner on System.in for values the user already passed on the command line.Scanner.close() closes the underlying stream. Closing a scanner on System.in closes stdin for the rest of the process — later reads fail.
Scanner on a File or a stream you own. Use try-with-resources.System.in in a simple homework main. Leave it open, or share one scanner for the process.try (Scanner in = new Scanner(Path.of("data.txt"))) {
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}
nextInt and nextLine without consuming the newline.nextInt in a loop without hasNextInt and blowing up on the first word.System.in by accident.== to compare a line to a literal after forgetting trim() / case.out in a program whose output is parsed by another tool.Scanner is not thread-safe and is slow for huge files — that is a later performance note, not an excuse to skip hasNext.hasNext* or parse lines. Treat mismatch as user error, not a crash story.err with a non-zero exit if you are writing a real CLI (System.exit is optional in main; returning is enough for many launchers).Scanner on System.in. Close scanners on files.printf conversions must match types. Prefer %n.~ Good luck with your revision! ~