Scanner Class in Java - CodingAliens

Taking Input from the user in Java -

For taking input from the user,you need 3 steps to do it.
step 1) import Scanner package  

import java.util.Scanner;


step 2) Create object of Scanner Class
Scanner scan = new Scanner(System.in);


step 3) Assign the object value to  the variable
int amount = scan.nextInt();



Example : Taking Different Types of Input


import java.util.Scanner;

public class CodingAliens {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Hello..! Enter your name -");

        String name = scan.nextLine();

        System.out.println("Hello " + name + "..!"
                + " Please enter the principal amount -");

        int amount = scan.nextInt();

        System.out.println("Enter the rate percentage -");

        float rate = scan.nextFloat();

        System.out.println("Enter the time period -");

        int time = scan.nextInt();
        float interest = amount * time * rate;

        System.out.println("Your interest amount is - "
                + interest);
    }

}

Post a Comment

0 Comments