Reading input
Everything so far hardcoded its data. Real judged problems — on this platform, in interviews, in competitive programming — hand your program its data through standard input (stdin), the text stream a program can read at runtime, and grade whatever you print to standard output. Master this handshake and every DSA problem becomes: read, solve, print. Java's tool is Scanner:
import java.util.Scanner; Scanner in = new Scanner(System.in); int a = in.nextInt(); // next whole number double d = in.nextDouble(); // next decimal String w = in.next(); // next single word String line = in.nextLine(); // rest of the line
nextInt skips spaces and line breaks on its own, so "3 4" and "3\n4" both work. One classic trap: after nextInt(), a following nextLine() first consumes the leftover end-of-line, often returning "". Call in.nextLine() once to flush before reading a real line.
Code exercise · java
Run this. The Input panel already contains `3 4`. The program reads both ints from stdin and prints their sum. Change the input to other numbers and run again.
You now hold the whole toolkit
Look back at what each unit handed you:
- syntax, types, and the compiler (units 1-2)
- branching and loops (unit 3), methods (unit 4)
- classes, encapsulation, inheritance, interfaces (units 5-6)
- ArrayList, HashMap, HashSet (unit 7), generics and exceptions (unit 8)
- lambdas and streams (unit 9), big-O, StringBuilder, and the two core interview patterns (unit 10)
That is enough Java to work through the DSA platform's problem sets, where every solution is a class reading stdin and writing stdout, exactly like this lesson. Finish with the rite of passage below.
Code exercise · java
Your turn: FizzBuzz. For 1 through 15, print Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number itself otherwise. Check divisibility by 15 first.
Problem
You check whether two strings of length n are anagrams by building a frequency map of each (lesson 10-2) and comparing the maps. In big-O notation, what is the time complexity? Answer like O(n).