Reading Console Input with Scanner
Reading Console Input with Scanner
So far every value in your programs has been hard-coded. Real programs need to respond to the user — asking for a name, a number, or a choice. Java provides the Scanner class in the java.util package for exactly this purpose. It wraps any input stream (keyboard, file, network) and lets you pull data out one piece at a time.
Importing and Creating a Scanner
Before using Scanner you must import it, then create an instance connected to System.in — the standard input stream that represents the keyboard.
scanner.close() releases the underlying resource. For simple console programs this matters less, but it is a good habit to build now.
Reading Different Types of Data
Scanner has a family of next…() methods, each matching a Java primitive type:
nextLine()— reads the entire line up to and including the newline character, then returns the text without the newline.next()— reads the next token (a sequence of characters separated by whitespace). Stops at a space.nextInt()— reads and parses the next token as anint.nextDouble()— reads and parses the next token as adouble.nextBoolean()— readstrueorfalse(case-insensitive).nextLong(),nextFloat()— for larger or single-precision numbers.
When you call nextInt() the scanner reads characters until it sees whitespace, converts the text to an int, and returns it. The newline that the user pressed is left in the buffer — this is the source of the most common Scanner pitfall.
The nextLine() Pitfall
Imagine you want to read an integer and then a full line of text:
The program never waits for you to type a city because nextLine() instantly consumed the newline that nextInt() left behind. The fix is to add an extra scanner.nextLine() call right after the numeric read to flush that leftover newline:
nextInt(), nextDouble(), or any other token-based method with a subsequent nextLine(), add a blank scanner.nextLine() call in between to consume the dangling newline.
Reading Multiple Values in a Loop
A loop combined with Scanner lets you process any number of inputs. A common pattern is to read until the user types a sentinel value — a special input that signals the end:
Checking Whether Input Is Available
Before calling a read method you can verify that the next token is actually of the expected type using the corresponding has…() method. This prevents a InputMismatchException when the user types text where a number is expected:
hasNextInt() / hasNextDouble() when building interactive programs that must handle bad input gracefully. For quick homework programs or exercises the simple read is fine — but knowing this pattern exists will save you when inputs are unpredictable.
Summary
The Scanner class is your bridge between the keyboard and your program. Remember the key points: import java.util.Scanner, create an instance with new Scanner(System.in), use nextInt() / nextDouble() for numeric input and nextLine() for full-line text, flush the leftover newline after every token-based read that is followed by a nextLine(), and close the scanner when you are done. With this tool you can write interactive programs that respond to real user input — which is the foundation for the console calculator you will build at the end of this tutorial.