-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedReaderInput.java
More file actions
70 lines (60 loc) · 2.56 KB
/
Copy pathBufferedReaderInput.java
File metadata and controls
70 lines (60 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package Phase0_SetupAndFirstPrograms.Input;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* <h1>Taking Input — java.io.BufferedReader</h1>
*
* <p><code>BufferedReader</code> is the older (pre-Java-5) way to read input. It reads one whole
* LINE at a time as a String — you have to parse the result yourself.</p>
*
* <h2>Why use BufferedReader over Scanner?</h2>
* <ul>
* <li>Faster than <code>Scanner</code> because it does not do regex-based tokenizing.
* Commonly used in competitive programming for that reason.</li>
* <li>Larger buffer size by default (8192 chars vs Scanner's 1024).</li>
* </ul>
*
* <h2>Trade-offs</h2>
* <ul>
* <li>Returns Strings only. You must convert with <code>Integer.parseInt()</code>,
* <code>Double.parseDouble()</code>, etc.</li>
* <li>Throws checked <code>IOException</code>, so you must declare "throws" or wrap in try/catch.</li>
* <li>No built-in support for reading individual tokens of different types from the
* same line (you would split the line yourself).</li>
* </ul>
*
* @author Deepak Gupta
* @version 1.0
* @since 2026-05-21
*/
public class BufferedReaderInput {
public static void main(String[] args) throws IOException {
// System.in is a byte stream. We wrap it with InputStreamReader to convert
// bytes -> chars, and then with BufferedReader to read line-by-line efficiently.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 1) Read a full line as a String
System.out.print("Enter your full name: ");
String name = br.readLine();
// 2) Read a number - need explicit parsing
System.out.print("Enter your age : ");
int age = Integer.parseInt(br.readLine());
// 3) Read multiple tokens from one line - split manually
System.out.print("Enter three numbers separated by spaces: ");
String[] tokens = br.readLine().split("\\s+");
int a = Integer.parseInt(tokens[0]);
int b = Integer.parseInt(tokens[1]);
int c = Integer.parseInt(tokens[2]);
System.out.println();
System.out.println("Hello " + name + ", age " + age);
System.out.println("Sum of the three numbers = " + (a + b + c));
br.close();
// SAMPLE RUN
// Enter your full name: Deepak Gupta
// Enter your age : 25
// Enter three numbers separated by spaces: 10 20 30
//
// Hello Deepak Gupta, age 25
// Sum of the three numbers = 60
}
}