Input in java using scanner
Written by
Read input using Scanner in Java
nextInt():
Function: Receives Integer input and stores in respective integer variable.
Syntax:
int <variable name> = <object name>.nextInt();
Example:
Scanner dprs=new Scanner(System.in);
int x=dprs.nextInt();
nextFloat():
Function: Receives Float input and stores in respective float variable.
Syntax:
float <variable name> = <object name>.nextFloat();
Example:
Scanner ddlj=new Scanner(System.in);
float f=ddlj.nextFloat();
nextLong():
Function: Receives Long input and stores in respective long variable.
Syntax:
long<variable name> = <object name>.nextLong ();
Example:
Scanner ddlj=new Scanner(System.in);
long l=ddlj.nextLong();
nextDouble():
Function: Receives Double input and stores in respective double variable.
Syntax:
double <variable name> = <object name>.nextDouble();
Example:
Scanner soty=new Scanner(System.in);
double d=soty.nextDouble();
nextLine():
Function: Receives String input and stores in the respective String variable.
Syntax:
String <variable name> = <object name>.nextLine();
Example:
Scanner soty=new Scanner(System.in);
String s=soty.nextLine();
Sample Program using Scanner Class:
import java.util.*;
class ScannerDemo
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.println("\n Enter a Sentence: ");
String x=inp.nextLine();
System.out.println("Entered Text is: "+x);
}
}
Output: