Write Java program to Get different input from user


This program takes input from the user for various types of data such as String, char, int, and float. It then prints out the input values back to the user.

  • The program starts by importing the Scanner class from java.util package. Then a class named "Different_Input" is created, and the main() method is defined in it.
  • Inside the main() method, variables are declared to hold the user input data, such as name (String), gen (String), age (int), tot (int), per (float), and gra (char) .
  • Then a Scanner object "input" is created to take input from the user.
  • The program asks the user to enter their name, gender, age, total, percentage, and grade. The user input for name is taken using the nextLine() method, and for the rest, various methods like next(), nextInt(), and nextFloat() are used.
  • Finally, the program prints out the input values back to the user using the System.out.println() method, with suitable messages.

Source Code

import java.util.Scanner;
public class Different_Input
{
    public static void main(String args[])  throws Exception
    {
        String name,gen;
		char gra;
        int age,tot;
        float per;
 
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the Name : ");
        name = input.nextLine();         
        System.out.print("Enter Gender (Male/Female) : ");
        gen = input.next(); 
        System.out.print("Enter the Age : ");
        age = input.nextInt();
        System.out.print("Enter the Total : ");
        tot = input.nextInt();
        System.out.print("Enter the Percentage : ");
        per = input.nextFloat();
        System.out.print("Enter the Grade : ");
        gra = input.next().charAt(0);
 
        System.out.println("Name : " + name);
        System.out.println("Gender : " + gen);
        System.out.println("Age : " + age);
        System.out.println("Total : " + tot);
        System.out.println("Percentage : " + per);
        System.out.println("Grade : " + gra);
 
    }
}

Output

Enter the Name : Joes
Enter Gender (Male/Female) : Male
Enter the Age : 38
Enter the Total : 422
Enter the Percentage : 84.40
Enter the Grade : B
Name : Joes
Gender : Male
Age : 38
Total : 422
Percentage : 84.4
Grade : B