Commit 7a38286
committed
feat: demonstrate console input using System.in.read() (Demo3)
WHAT:
- Implemented a simple program that reads a single byte from the console
using `System.in.read()`.
- Casts the byte value to a character and prints it.
HOW IT WORKS:
1. `System.in` is a standard input stream connected to the keyboard.
2. `System.in.read()`:
- Reads a single byte of input as an `int` (0–255).
- Returns -1 if the end of the stream is reached.
3. The read byte is cast into a `char` to display the entered character.
4. Exceptions (e.g., I/O errors) are caught using a try-catch block.
KEY POINTS:
- Only reads **one character** at a time (first byte of input).
- If you type a character and press Enter, it captures the ASCII code of
the first character, while the Enter key may remain in the buffer.
- To read full lines or strings, classes like `BufferedReader` or
`Scanner` are more practical.
REAL-WORLD USE CASES:
- Learning low-level I/O operations in Java.
- Useful for understanding how streams work before moving to higher-level
input handling.
- Could be applied in
simple console-based
utilities where
byte-level
input is sufficient.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7c49837 commit 7a38286
1 file changed
+6
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 4 | + | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
| 14 | + | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
| 20 | + | |
0 commit comments