Tuesday, July 9, 2013

GET THE INPUT THROUGH KEYBOARD-1

We can get the input for a program using the Scanner class and Input Stream reader class.

USING SCANNER CLASS:( A Class in Util Package)
~~~~~~~~~~~~~~~~~~~~~
REFER LINK FOR MORE DETAILS ABOUT SCANNER CLASS: ClickHere
import java.io.*;
import java.util.*;                     //util package
class input
{
   public static void main(String h[])
{

Scanner ob=new Scanner(System.in);// <class name> <object name>=new <constructor(System.in)>
System.out.println("ENTER STRING VALUE");
String a=ob.next( );      //function to get the string inputs
System.out.println("ENTER A INT  VALUE");
int i=ob.nextInt( );        //function to read the int  inputs
System.out.println("ENTER FLOAT VALUE");
float f=ob.nextFloat( );       /function to read the float  inputs
System.out.println("ENTER DOUBLE VALUE");
double d=ob.nextDouble( );
System.out.print("ENTER BYTE VALUE");
byte b=ob.nextByte( );
//PRINTING THE INPUT VALUES
System.out.println("ENTERED STRING VALUE IS"+a);
System.out.println("ENTERED INT VALUE IS"+i);
System.out.println("ENTERED FLOAT VALUE IS"+f);
System.out.println("ENTERED DOUBLE VALUE IS"+d);
System.out.println("ENTERED BYTE VALUE IS"+b);
}
}


EXPLANATION:
~~~~~~~~~~~~~
Scanner is an class in the util pakage;

ob ->object created for the Scanner class

next( ),nextInt( ),nextFloat( )-> predefined functions in the util class(refer util class link given above).


+  ->concat the two different strings


No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips