SCANNER Class in Java – Part(i)

The Scanner class is mainly used to get the user input, and it belongs to the java.util package. In order to use the Scanner class, you can create an object of the class and use any of the Scanner class methods.

SCANNER CLASS METHODS :

nextBoolean – reads a boolean value from the user

nextByte() – reads a byte value from the user

nextDouble() – reads a Double value from the user

nextFloat() – reads a float value from the user

nextInt() – reads a int value from the user

nextLine() – reads a string value from the user

nextLong() – reads a long value from the user

nextShort() – reads a short value from the user

HOW WE CREATE A OBJECT IN CLASS?

==> classname objectname = new classname();

HOW WE CREATE A OBJECT IN SCANNER CLASS ?

==> Scanner objectname = new Scanner(system.in)

WHY SHOULD WE CREATE OBJECT IN SCANNER CLASS ?

Scanner class contains some pre defined methods inside in it. if we want to access that method, we need some memory,so that we create object in scanner class.

WHY SHOULD WE USE import java.util.scanner ?

There are lot of scanner class file in jdk folder in different folder name. so if we want to access the specific class file in that specific folder,we should import that specific folder, that’s why we should import that specific folder name import java.util.scanner

CREATEING A ELECTRICITY BILL PROGRAM BY USING SCANNER CLASS:

import java.util.Scanner;
class ElectricityBill{
public static void main(String[] args)
{
System.out.println(“TAMILNADU ELECTRICITY BOARD”);

Scanner details = new Scanner(System.in);

System.out.println(“Enter name of the customer : “);
String n1 = details.nextLine();

System.out.println(“ADDRESS of the customer: “);
String n2 = details.nextLine();

System.out.println(“customer number : ” );
String n3 = details.nextLine();

ElectricityBill amountToPaid = new ElectricityBill();
int amount = amountToPaid.pay();
System.out.println(“total is ” +amount);
}

private int pay()
{
Scanner amountpay = new Scanner(System.in);

Output:

OUTPUT:

TAMILNADU ELECTRICITY BOARD
Enter name of the customer :
vineeth
ADDRESS of the customer:
chennai -21
customer number :
67567897
january Month:
567
february Month:
765
march Month:
897
april Month:
965
may Month:
845
total is 4039