Video in progress
When do you choose Scanner, and when BufferedReader?
Scanner is a convenient parser: nextInt(), nextDouble(), it works with delimiters, but it is slow. BufferedReader is fast, gives you only lines, and you do the parsing yourself. For large volumes and server code, BufferedReader.
📖 Answer
Scanner uses regex inside, and every next...() call is expensive. For learning exercises and small console programs it is very convenient:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
But if you read a CSV with a million lines using Scanner, it will be several times slower than BufferedReader + split. That is also why people use BufferedReader in competitive programming.
Another difference: Scanner swallows IOException; to find out about an error you have to check sc.ioException() separately. BufferedReader throws it openly.
The trap: if you call nextLine() after nextInt(), you get an empty string, because the \n after the number hasn't been read yet.
Sign up to mark studied questions and save your progress.