Nearby lessons
15 of 125Java - User Input (Scanner)
📌 What You Will Learn
- What a stream is and how data flows
- Byte streams vs character streams
- Reading and writing text files easily
- The modern File methods and Scanner input
- Serialization — saving an object to a file
User Input (Scanner) is a core concept of the Java language. This lesson explains Reading Input from the Keyboard with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Reading Input from the Keyboard
Old way — BufferedReader
Modern easy way — Scanner (Java 5+)
The Scanner class is much simpler. It can read words, whole lines, and numbers directly.
Example01
Reading Input from the Keyboard
Trainer's Note: Trainer tip: nextLine() reads a whole line, next() reads only one word, nextInt() reads an int, nextDouble() reads a decimal. The Scanner is what most beginners and college projects use today.
Example02
📝 Key Takeaways
- A stream is a flow of data: input brings data in, output sends it out.
- Byte streams (InputStream/OutputStream) for images/videos; character streams (Reader/Writer) for text.
- Wrap FileReader in BufferedReader to read text line by line.
- Scanner is the simplest way to read input from the keyboard.
- The File class gives file information; it does not read data.
- Serialization saves objects to files; transient fields are not saved.
- Always close streams — or better, use try-with-resources.
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1