0% found this document useful (0 votes)
6 views3 pages

2 (Java)

This document outlines two Java programs that demonstrate accepting user input through the BufferedReader and Scanner classes. The first program checks if an integer is a two-digit number, while the second program prints the percentage range based on a student's grade. Both programs successfully illustrate basic input handling in Java.

Uploaded by

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

2 (Java)

This document outlines two Java programs that demonstrate accepting user input through the BufferedReader and Scanner classes. The first program checks if an integer is a two-digit number, while the second program prints the percentage range based on a student's grade. Both programs successfully illustrate basic input handling in Java.

Uploaded by

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

Name: Shaikh Salif Aminuddin

CLASS: S2 ROLL NO: 2201094

EXPERIMENT NO: 2

AIM: Program on accepting input through keyboard.

THEORY: BufferedReader ClassThe BufferedReader class of Java is used to


read the stream of characters from the specified source (character-input stream).
The constructor of this class accepts an InputStream object as a parameter. This
class provides a method named read() and readLine() which reads and returns
the character and next line from the source (respectively) and returns them.
 Instantiate an InputStreamReader class bypassing your InputStream object as
a parameter.
 Then, we have to create a BufferedReader, bypassing the above obtained
InputStreamReader object as a parameter.
 Now, read data from the current reader as String using the readLine() or read()
method.
 Scanner ClassScanner is a class in java.util package used for obtaining the
input of the primitive types like int, double, etc. and strings. It is the easiest way
to read input in a Java program, though not very efficient if you want an input
method for scenarios where time is a constraint like in competitive
programming.
 To create an object of Scanner class, we usually pass the predefined object
System.in, which represents the standard input stream. We may pass an object
of class File if we want to read input from a file.
 To read numerical values of a certain data type XYZ, the function to use is
nextXYZ(). For example, to read a value of type short, we can use nextShort()
 To read strings, we use nextLine().  To read a single character, we use
next().charAt(0). next() function returns the next token/word in the input as a
string and charAt(0) function returns the first character in that string.

CODE-01:
1. WAP to check if an integer (Accepted from user via BufferedReader
Class) is atwo-digit number or not.
import java.io.*;
class IntegerCheck {
public static void main(String args[]) throws IOException
{
System.out.println("Name = Shaikh Salif\nDiv : S2\nRoll No. :
94");
BufferedReader reader =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a number : ");
int number = Integer.parseInt(reader.readLine());
int i=number;
int j=0;
while(i>0) {
i=i/10;
j++;
}
if(j<=2)
{
System.out.println("Number is less than 100");
}
else {
System.out.println("Number is greater than 100");
}
}
}

OUTPUT:
CODE-02: WAP to print the Percentage range of a student as per following
criteria for thegrade accepted via Scanner Class.
import java.util.Scanner;
class StudentGrades {public static void main(String[] args)
{
System.out.println("Name = Shaikh Salif\nDiv : S2\nRoll No. : 94");
Scanner sc= new Scanner(System.in);
System.out.print("Enter Grade : ");
char grade = sc.next().charAt(0);
switch(grade) {
case 'A': System.out.print("91-100");
break;
case 'B': System.out.print("81-90");
break;
case 'C': System.out.print("71-80");
break;
case 'D': System.out.print("61-70");
break;
case 'F': System.out.print("0-60");
break;
}
}

OUTPUT:

Conclusion: Basic program in JAVA by taking input from user is successfully


implemented in above two programs as illustrated.

You might also like