Java Program to demonstrate Scanner Class
Last updated:14th Dec 2021
In this program, we use different methods to take input from the user. like taking String, Integer, and Float input.
java scanner demonstration program
import java.util.Scanner;
class ScannerDemo
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("enter string");
String data1 = input.nextLine();
System.out.println("enter integer");
int data2 = input.nextInt();
System.out.println("enter float value");
float data3=input.nextFloat();
System.out.println("String line: "+data1);
System.out.println("integer value: "+data2);
System.out.println("float value: "+data3);
input.close();
}
}
output
C:\gaurav>javac ScannerDemo.java C:\gaurav>java ScannerDemo enter string gaurav enter integer 15 enter float value 12.5 String line: gaurav integer value: 15 float value: 12.5