Showing posts with label Input from User. Show all posts
Showing posts with label Input from User. Show all posts

Friday, 13 June 2014

INPUT FROM USER


TAKING INPUT FROM USER :

Like in C++  .  cout and cin commands were contained by library iostream.h 
And to use cin and cout commands in a program , we used to write :
            
                    #include <iostream.h>

In the similar way , for taking input from user in JAVA , Scanner class is used and this is contained in in-built package      util . 


We will see how scanner method is used :
                 



                                      import java.util.*;
               class ScanDemo
               {
                     public static void main(String args[])
                     {
                          Scanner s = new Scanner(System.in);
                          // an object s of scanner class is                                  created
                           
                           System.out.println("Num1");
                           int a = s.nextInt();
                           System.out.println("Num2");
                           int b = s.nextInt();
                           System.out.println(a+b);
                     }
               }



nextInt is an inbuilt method in scanner class which takes integer as input . 

The input from user can be taken in form of character or string or can have any data type .

Like in C++ keyword #include was used to access the classes of any library , in JAVA keyword import is used for including any in-built class.

We will see later in PACKAGES how in-built classes are kept in packages and how to acces and create package  

To refer how to take other data types as input from user please refer :


Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes