Fallowing icon

Monday, October 1, 2012

Java Scanner-get user Input


Get user input using Scanner


   Before we learned java programs, those we got inputs in programs. There are number of ways have to get user inputs in java. Scanner and BufferedReader are some of them.

In this lesson we learn how to get user input from user. Scanner class is already build in java.util package. We can import java.util.Scanner class and create Scanner object. Scanner is allowed to input all primitive data type values and String data type values.


Import Scanner class to java program -   

   We use import statement above the class body. 

  import java.util.Scanner;

Create a Scanner object -

   Before we are learned how to create object. Now we create object is using Scanner class. 

      
       Scanner s=new Scanner(System.in);


   Scanner s is a variable which reference data type. System.in is input stream it means that scanner will take input from console.

   
Get values -

    We have created Scanner object. When you want to get values in program, we want to call applicable methods using this object.

If you want to get - byte data type value    - nextByte();
                     Request the next input as a byte


                   - Short data type value   - nextShort();
                     Request the next input as a Short

                   - Integer data type value - nextInt();
                     Request the next input as an Integer

                   - float data type value   - nextFloat();
                     Request the next input as a float

                   - double data type value  - nextDouble();
                     Request the next input as a double

                   - String data type value  - next();
                     Request the next complete token from this
                     scanner and returns it as a String a token
                     is usually ended by white space such as a
                     blank or line break

                   - String data type value  - nextLine();
                     Request the rest of the current line,
                     excluding any line separator at the end

Example -       
   

             import java.util.Scanner;

             class Test{
                    String firstName,secondName,thirdName;
                    int firstAge,secondAge,thirdAge;
    
                    void getValue(){                                                   //declare the getValue method 
                            Scanner b=new Scanner(System.in);
        
                            System.out.print("Enter first Name   -");
                                    firstName=b.nextLine();       
                            System.out.print("Enter second Name  -");
                                    secondName=b.nextLine();
                            System.out.print("Enter third Name   -");
                                    thirdName=b.nextLine();
        
                            System.out.print("Enter first Age    -");
                                    secondAge=b.nextInt();
                            System.out.print("Enter second Age   -");
                                    firstAge=b.nextInt();
                            System.out.print("Enter third Age    -");
                                    thirdAge=b.nextInt();         
                    }
    
                   void printData(){                                                     //declare the printData method
        
                              System.out.print("\n\nFirst name   ="+firstName+" his/her age ="+firstAge+"\n"+"Second name  ="+secondName+" his/her age ="+secondAge+"\n"+"Third name   ="+thirdName+" his/her age ="+thirdAge);              //use \n for get new line 
                    }   
    
                     public static void main(String[] args) {
                                Test a=new Test();                                                        //create object
                                a.getValue();                                                                //call getValue method
                                a.printData();                                                                //call print method
                    }
              }






     

Discussion - 


  We create getValue and printData methods in this example. Then We declare the Scanner object in getValue method. printData method is used for print values, We created single print statement and give all values. \n is used for get new line in this statement. 

  When this program is running, first print the word Enter first Name. Then cursor is blinding but program is not running beyond.
It mean is now program is request the input. Program is not run beyond give value for it. program request three names and three ages for that names like that.

  After the execute getValue method then execute printData method. In that time program execute print statement.

   
   

1 comment:

  1. please, add more tutorial like.... interface, exception handling,package,string.... etc

    ReplyDelete