How to get User Input from User in java??
program to get User Input in java
import java.util.Scanner;
public class UserInput
{
public static void main(String
args[])
{
Scanner obj=new
Scanner(System.in);
int number;
String str;
float f;
System.out.println("Enter the
String? :");
str=obj.nextLine();
System.out.println("You have
entered the string:"+str);
System.out.println("Enter the
number ?");
number=obj.nextInt();
System.out.println("You have
entered the number:"+number);
System.out.println("ENter the
float number");
f=obj.nextFloat();
System.out.println("YOu have
entered the float value:"+f);
}
}
java.util has a
class named “scanner” which is used to read different types of
values from users.
Scanner obj=new Scanner(System.in);
In this statement
Scanner is the class name,obj is the Object of the Scanner class and
System.in is the input stream.
str=obj.nextLine();
nextLine() is used
to get a line from the user,similarly nextInt() and nextFloar are
used to get Integers and float from the users
Comments
Post a Comment