0% found this document useful (0 votes)
10 views5 pages

Experiment 2

Uploaded by

sheetdbtest2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Experiment 2

Uploaded by

sheetdbtest2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Anish Dharnidhar AI & DS Batch S23 Roll no : 129

Experiment 2
Aim: Programs on accepting input through keyboard (LO1)
Theory

Scanner class

The Scanner class in Java, part of the java.util package, is a versatile and easy-to-use
class
designed for parsing primitive types and strings using regular expressions.
Introduced in Java 5, it is widely used for reading input from various sources,
such as the console, files, and strings.

BufferedReader class
The BufferedReader class, part of the java.io package, is designed to efficiently
read text from character input streams, such as files or network connections. It
buffers the input to provide efficient reading of characters, arrays, and lines.

Scanner BufferedReader

Parsing and reading primitive Reading text from character input


types and strings using regular streams efficiently. Suitable for
expressions. Suitable for simple reading large text files or network
console-based input. streams.

Can read from InputStream, File, Typically used with character input
Path, and String. streams like FileReader or
InputStreamReader.

Generally slower due to parsing Faster for reading large text


overhead, especially with large files due to efficient buffering.
input data.

Provides methods like nextInt(), Focuses on reading text with


nextLine(), nextDouble(), making it methods like read() and
easier to use for beginners. readLine(), requiring additional
parsing for different data types.

Throws InputMismatchException Throws IOException, requiring


for type mismatches and explicit exception handling. Suitable
NoSuchElementException for robust
Anish Dharnidhar AI & DS Batch S23 Roll no : 129

for no input. Handles more applications needing precise


exceptions internally. control over I/O operations.

1. Program on taking an input from a user using BufferedReader and check whether it
is a 2- digit number or not.

Code:

Output:

2. WAP on taking an input character Grade from Scanner object and printing its
corresponding percentage as below:
Percentage Grade 0-60 F
61-70 D
71-80 C
81-90 B
91-100 A
(rest grades can be printed as Out of Range)
Anish Dharnidhar AI & DS Batch S23 Roll no : 129

Code:

Output:
Anish Dharnidhar AI & DS Batch S23 Roll no : 129

3. WAP to read and display details of Student : Name, Age, Address, Branch, Batch,
PhoneNumber using Scanner class.

Code:
Anish Dharnidhar AI & DS Batch S23 Roll no : 129

Output:

You might also like