javat4

Shubham GordeShubham Gorde
1 min read

Taking Inputs in java

So basically to take input you have import essential library which Scanner. Then you have create its object to access its properties and methods.

You can use objectName.nextInt(); // for int

objectName.nextFloat(); // for float and many more

So this is how you can take inputs from the user in java through command line.

import java.util.Scanner;

public class TakingInput {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name : ");
        String name = sc.nextLine();
        System.out.println();

        System.out.println("Enter First number : ");
        int a = sc.nextInt();

        System.out.println("Enter second number in decimal : ");
        float b = sc.nextFloat();

        float sum = a + b;
        System.out.println(sum);
    }
}
0
Subscribe to my newsletter

Read articles from Shubham Gorde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Gorde
Shubham Gorde