Sunday, 10 July 2016

CORE JAVA(ch-3,taking input from user)

 Hello guys!This is hitesh,and today we will discuss and learn about buffered reader and scanner classes.

  • These classes are used to take input from user!
  • They are used for different purpose.


->We will also learn about Classes and Objects.

Getting Interactive Input from User
-         Java provides built-in classes for data input
o   Scanner class under java.util package
o   BufferedReader class under java.io package

Package
A package is a folder which contains related set of classes.
1.    java.lang
a.     Default package provides commonly used classes
b.    System, Math, String etc.
2.    java.util
a.     Provides collections and utility
b.    LinkedList, Array, Stack
c.     Date, Scanner, DateFormat
3.    java.io
a.     Provides classes for input and output with file handling
b.    File, FileReader, FileWriter
c.     BufferedReader, InputStreamReader

Before using any class of a package we need to import the class into the program using import keyword.
Syntax 1: for single class
import <packagename.classname>;
Syntax 2: for all the classes
import <packagename.*>;


These classes contains two kinds of members
1.    Static or class members
2.    Non-static or instance members
The static members are the common members which do not require an instance of a class and called directly with the class name.
Math.pow(n,p)

Non-static members always need an instance to call them.
String s="Amit Kumar";
s.toUpperCase();

Note:
Reference is also a kind of variable related with some class.
Example
double num; //variable – to hold some data
Double num; //reference – to hold some address

What is class?
A set of specifications or blueprint about an entity describing all possible data members and the methods applicable on that entity to create similar kind of multiple objects.
Use class keyword to define a class.


What is instance or object?
The real entity created based on class specifications is called as instance.
To create an instance we need special method called as constructor along with new.

The constructor is a method having some special features
1.    Same name as class name
2.    No return type
3.    Used to initialize data members of an instance
Example
String s=new String(“Amit Kumar”);

Full Code
public class Test
{
    public static void main(String args[])
    {
        System.out.println(new String("Vikas Kumar").toUpperCase());
       
        String name=new String("Kapil Kumar");
        System.out.println(name.toUpperCase());
        System.out.println(name.toLowerCase());
        System.out.println(name.length());
    }
}

Using Scanner class
-         To input the data from user
-         It provides various methods
o   String next()
o   int nextInt()
o   float nextFloat()
o   double nextDouble()
o   long nextLong()
-         Before using any such function create an instance of Scanner class

Scanner sc=new Scanner(System.in);
Example
WAP to input a number and its power then show number to the power.

import java.util.Scanner;
public class PowerTest
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Number : ");
        double n=s.nextDouble();
        System.out.print("Power : ");
        double p=s.nextDouble();
       
        double r=Math.pow(n, p);
       
        System.out.printf("%.2f to the power %.2f is %.2f", n,p,r);
    }
}

Ex-2:WAP to insert age and name of person and check it to be a valid voter.

import java.util.Scanner;
public class VoterTest
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Name : ");
        String name=s.next();
        System.out.print("Age : ");
        int age=s.nextInt();
       
        if(age>=18)
            System.out.printf("%s you can vote", name);
        else
            System.out.printf("%s you cannot vote",name);
    }
}
The above example was based on scanner class..lets do it with the help of buffered reader. 

Using import static keyword
Keywords used to import static members of a class to avoid the use of the class name again and again.

Syntax

import static <packagename>.<classname>.membername;
or
import static <packagename>.<classname>.*;

Example
import java.util.Scanner;
import static java.lang.System.*;
public class VoterTest
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(in);
        out.print("Name : ");
        String name=s.next();
        out.print("Age : ");
        int age=s.nextInt();
       
        if(age>=18)
            out.printf("%s you can vote", name);
        else
            out.printf("%s you cannot vote",name);
    }
}

Using BufferedReader class
A class from java.io package to read data from anywhere.
When we press a key from keyboard (System.in), it provides stream of bits. This stream of bits get passed to another class called as InputStreamReader which converts the stream into readable character format.
These character get assembled into memory until we press the enter using another class called as BufferedReader. It provides readLine() method to read the data in the buffer.
            String readLine() throws IOException

import java.util.Scanner;
import static java.lang.System.*;
import java.io.*;
public class VoterTest1
{
    public static void main(String args[]) throws IOException
    {
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
       
        Scanner s=new Scanner(in);
        out.print("Name : ");
        String name=br.readLine();
        out.print("Age : ");
        int age=s.nextInt();
       
        if(age>=18)
            out.printf("%s you can vote", name);
        else
            out.printf("%s you cannot vote",name);
    }
}

So,this was like scanf and gets of C language, where we use scanner class as scanf() and buffered reader as gets().

In the next post we'll learn how to create our own static members and what are wrapper classes.


Thank you
Keep learning :)
keep coding :) 




No comments:

Post a Comment