Home »
Java Programs »
Core Java Example Programs
Java program to get input from user
Using Scanner Class:
Scanner class is used to get input from the user, to use this class, you will have to import java.util.scanner package.
Get Input from User program in Java
Common functions to get input
Method | Description |
next() |
to get string without space from the user |
nextLine() |
to get string with spaces from the user |
nextInt() |
to get integer value from the user |
nextFloat() |
to get float value from the user |
nextByte() |
to get byte value from the user |
In this program we will read Name, Gender, Age and Float and then print all inputted values.
//program to input int, float and string value
import java.util.Scanner;
public class GetIpAddress
{
public static void main(String args[]) throws Exception
{
String name, gender;
int age;
float weight;
Scanner SC=new Scanner(System.in);
System.out.print("Enter name: ");
name= SC.nextLine();
System.out.print("Enter Gender (Male/Female): ");
gender=SC.next();
System.out.print("Enter age: ");
age=SC.nextInt();
System.out.print("Enter weight: ");
weight=SC.nextFloat();
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Weight: " + weight);
}
}
Output
Enter name: Mike the Expert
Enter Gender (Male/Female): Male
Enter age: 21
Enter weight: 65.50
Name: Mike the Expert
Gender: Male
Age: 21
Weight: 65.5
Core Java Example Programs »