Skip to main content

Java Inputs

You can already show the data to the users.

Now let's receive it from user.

In java reading the input is a bit complicated

You have to use the Scanner class as show here

import java.util.Scanner;

public class LearnInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Enter your age: ");
        int age = sc.nextInt();
        System.out.println("Enter your salary: ");
        double salary = sc.nextDouble();
        System.out.println("Your name is " + name);
        System.out.println("Your age is " + age);
        System.out.println("Your salary is " + salary);
        sc.close();
    }
}

 

// Code Explanation

First we import the java.util.Scanner to use it.

Line 5 : We create a Scanner variable