Friday, January 20, 2017

Input Functions In Scanner Class Java

Tags

We can use the following Functions



Method Inputs
nextInt() Integer
nextFloat() Float
nextDouble() Double
nextLong() Long
nextShort() Short
next() Single word
nextLine() Line of Strings
nextBoolean() Boolean

Following will help you understand:

import java.util.Scanner;    //package for Scanner Class
public class Inputtest
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);   //Object of Scanner Class
        System.out.println("Enter Any Integer");
        int x=input.nextInt();                                  //for integer
     
        System.out.println("Enter Any String");
        String name=input.next();                         //for String
     
        System.out.println("Enter Any Double Value");
        double d=input.nextDouble();                  //for Double
     
        System.out.println("Enter Any Character");
        char c = input.next().charAt(0);               //for Character
     
        System.out.println("Enter Any Float");
        float f=input.nextFloat();                         //for float
    }
 
}

Output: