Ad space (banner)
โ˜•Java Lessons
Lesson 58 / 68

Reading from Standard Input

This lesson covers receiving standard input in Java using Scanner, so you can build a program that processes user input. It's for anyone searching "Java Scanner usage."

The Scanner class lets you receive input (standard input) from a keyboard or similar source. Use it when you want a command-line tool to prompt the user for something.

The example creates a Scanner with new Scanner(System.in), reads one line of input into name with scanner.nextLine(), and uses it in a greeting. Notice System.in represents keyboard input.

A common early mistake is choosing the wrong method for the type of input you're reading. You need to pick the right method โ€” nextInt(), nextLine(), and so on โ€” based on the input's type; mixing them can produce unexpected behavior.

In real projects, using an input value without validating it first can lead to unexpected errors, so combining it with a validation step is safer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)