OOPM JAVA - Use of Scanner class and BufferedReader class

Experiment No. 01

Aim : Write a program to input your roll no, name, age, fees paid, gender from keyboard and print them.(Use of Scanner class and BufferedReader class)


Objective : To make the students aware of different primitive data types and there use in real world .

Outcome : Students have come to know the different type of primitive data types of java. They have learned how to make use of these in real world scenario.
Students are exposed to reading of input data from consol.
Theory: 
Data Types in Java
In java, there are two types of data types
1. Primitive data types
2. Non-primitive data types




      Data Type

      Default Value

      Default size

Boolean

false

1 bit

Char

'\u0000'

2 byte

Byte

0

1 byte

Short

0

2 byte

Int

0

4 byte

Long

0L

8 byte

Float

0.0f

4 byte

Double

0.0d

8 byte


Algorithm:

  • Start
  • Create object of BufferedReader class.
  • int a=(Integer.parseInt)br.readLine().
  • float b=(Float.parseFloat)br.readLine();
  • double c=(Double.parseDouble) br.readLine();
  • String d= br.readLine();
  • Display a,b,c,d
  • Create object of Scanner class
  • int p=sc.nextInt()
  • float q=sc.nextFloat()
  • double r=sc.nextDouble()
  • String s=sc.nextLine()
  • Display p,q,r,s
  • Stop

CODE : 
 
import java.io.*; 
import java.util.Scanner; 
public class StudentDetails 
 static String name, gender; 
 static int age, roll, fees; 
 public static void main(String args[]) throws Exception 
 { 
 BufferedReader br = new BufferedReader(new 
InputStreamReader(System.in)); 
 System.out.print("Enter name: "); 
 name = br.readLine(); 
 System.out.print("Enter Gender (Male/Female): ");
gender = br.readLine(); 
 System.out.print("Fees paid in Rupees : "); 
 fees = Integer.parseInt(br.readLine()); 
 System.out.print("Enter roll no. : "); 
 roll = Integer.parseInt(br.readLine()); 
 System.out.print("Enter age: "); 
 age = Integer.parseInt(br.readLine()); 
 display(); 
 
 Scanner SC = new Scanner(System.in);//System.in is a standard input 
stream 
 System.out.print("\nEnter name: "); 
 name = SC.nextLine(); //reads string 
 System.out.print("Enter Gender (Male/Female): "); 
 gender = SC.nextLine(); 
 System.out.print("Fees paid in Rupees : "); 
 fees = SC.nextInt(); 
 System.out.print("Enter roll no. : "); 
 roll = SC.nextInt(); 
 System.out.print("Enter age: "); 
 age = SC.nextInt(); 
 display(); 
 } 
 public static void display() //Displaying the details entered by the user 
 { 
 System.out.println("\nStudent details are:"); 
 System.out.println("Name: " + name); 
 System.out.println("Roll No : " + roll); 
 System.out.println("Gender: " + gender);
System.out.println("Age: " + age); 
 System.out.println("Fees paid in Rupees: " + fees); 
 } 
}


Output:

By BufferedReader class

By Scanner class

(App used for compiling: Dcoder
Dcoder is a very simple and useful mobile app which has most of the languages in it. So give this app a try if you want to learn coding.)

Question of Curiosity: 
1. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java? 
Ans:- ASCII and ISO-LATIN-1 occupy first 0 to 127 in Unicode character set used for characters in Java. 
 
2. What are the default or implicitly assigned values for data types in java? 
Byte. : 0 
Short. : 0 
Int : 0 
Long : 0L 
Float. : 0.0f 
Double : 0.0d 
Char. : ‘u0000’ 
String (or any object) : Null 
Boolean. : False 
 
3. What is casting? 
Ans:- Casting refers to the assignment of one data type to another .There are two types of casting ie widening cast & narrowing cast. WideningCast refers to increasing a variable’s storage capacity. Narrowing cast refers to the Decreasing the variable’s storage capacity. 
 
4. What is difference between Scanner and BufferReader Class in Java? 
Ans:- A Scanner Class is a very powerful utility than BufferReader class. Scanner class can parse the user input and reads all the data types apart from the String. BufferReader Class can only read String in java. Scanner Class uses regular expression to read and prase text input. BufferReader is synchronised while Scanner is not synchronised i.e BufferReader objects can be shared between multiple threads. 
 
5. What is the difference between Data Type and Data Structure? 
Ans:- 
A data type is type of information transmitted between the programmer and the compiler where programmer informs the type of data to be stored and also how much space is required in memory.
A Data Structure is a collection of different forms and different types of data that has a set of specific operations that can be performed.
 
Similar Programs : 
1. Write a program to input Emp no, name, age, salary, sex from keyboard and print 
them.(Use of Scanner class or BufferedReader class) 
2. Write a program to input Bank Branch No, Man_name, age, salary, address,sex from 
keyboard and print them.(Use of Scanner class or BufferedReader class) 
3. Write a program to input your CollegeName, year of starting, affiliated to, fees of 
admission, grade from keyboard and print them.(Use of Scanner class or 
BufferedReader class) 
4. Write a program to input bookid, Title, vol, edition, no_of_pages, price, typeofbook 
from keyboard and print them.(Use of Scanner class or BufferedReader class) 
5. Write a program to input train no, train name, source, destination, no-kms, no_of 
coaches, type of train, fair from keyboard and print them. 

Comments