C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Read and display user input - Java Program

This program will read:

  • A String
  • An integer
  • A float

And display them back to the user.

import java.util.Scanner;

 

public class ReadAndDisplay {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        // Reading a string (name)

        System.out.print("Enter your name: ");

        String name = sc.nextLine();

 

        // Reading an integer (age)

        System.out.print("Enter your age: ");

        int age = sc.nextInt();

 

        // Reading a floating value (salary)

        System.out.print("Enter your salary: ");

        float salary = sc.nextFloat();

 

        // Displaying all inputs

        System.out.println("\n---- User Details ----");

        System.out.println("Name   : " + name);

        System.out.println("Age    : " + age);

        System.out.println("Salary : " + salary);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter your name: Anand Rao
Enter your age: 28
Enter your salary: 45000.50

---- User Details ----
Name   : Anand Rao
Age    : 28
Salary : 45000.5

Explanation (Step-by-Step)

Step 1 — Create Scanner

Scanner sc = new Scanner(System.in);

Allows reading input from keyboard.

Step 2 — Read a String

String name = sc.nextLine();

Reads whole line including spaces (e.g., "Anand Rao").

Step 3 — Read an Integer

int age = sc.nextInt();

Reads an integer value.

Step 4 — Read a Float

float salary = sc.nextFloat();

Reads a decimal number.

Step 5 — Display Input Back to User

System.out.println("Name: " + name);

Prints all values exactly as entered.