You are on page 1of 11

 To input data in a program using appropriate

methods
Students should be able to:
 identify the prerequisites for data input
 apply the correct method for the appropriate
data type
Input by using Scanner class.
Scanner Class allows the user to read values of various types
from the keyboard and other devices.

The Scanner class is defined within the Java Package “util”

In order to make use of the class in a Java program, you need


to include it by typing the following in the beginning of your
Java program.

import java.util.*;
Or
import java.util.Scanner;
 In order to make use of the Scanner class, it is
necessary to create an object as shown below:
(An object is a special type of variable
which will be discussed in Grade 10)

Scanner sc=new Scanner(System.in);

Scanner - An in-built class


sc (can be anything) - object name
new - a keyword that
allocates memory for
the object
System.in – refers to the keyboard
Methods to read tokens using the Scanner object.

Scanner class has the following methods to read data of


different data types. The method names must be typed as
shown below.
next().charAt(0) : reads a character
next() : reads a String without spaces
nextLine() : reads a String that can have spaces
nextInt() : reads data of int type
nextByte () : reads data of byte type
nextShort() : reads data of short type
nextLong() : reads data of long type
nextFloat () : reads data of float type
nextDouble() : reads data of double type
nextBoolean() : reads data of boolean type
Scanner in = new Scanner(System.in); // creating a Scanner object
General Syntax:
<variable of a particular type>=<Scanner Object>.next<data type>;
Examples:
int p;
p = in.nextInt();
double b2;
b2= in.nextDouble();
String st = in.nextLine();
byte b = in.nextByte();
float b1 = in.nextFloat();
long n = in.nextLong();
short x= in.nextShort();
A sample program that accepts a character and numbers

import java.util.Scanner;
class MATH {
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 2 numbers : ");
int n = sc.nextInt();
int m =sc.nextInt()
System.out.print(“You want to add or subtract …..enter + or - :");
char ch = sc.next().charAt(0);
if (ch == ‘+' )
System.out.println(“Sum :“ + (m+n));
else if (ch == ‘-’)
System.out.println(“Difference : “+ (m-n));
else
System.out.println("Invalid operator");
} }
Write a program that accepts the name and the
basic salary and calculates the allowances and
deductions as shown below.

Dearness allowance = 25 % of Basic pay.


House rent Allowance = 15 % of basic pay.
Provident fund = 8.33 % of Basic pay.
Net Pay = Basic Pay + dearness allowance +
House rent.
Gross pay = Net pay – Provident Fund.
import java.util.Scanner;
class Employee
{
public static void main()
{
Scanner in = new Scanner(System.in);
double da, hra, pf, npay, gross;
int basic;
String st;
System.out.println(“Enter name of the employee”);
st=in.nextLine();
System.out.println(“Enter the basic salary”);
basic=in.nextInt();
da=basic*25/100;
hra=basic*15/100;
pf=basic*8.33/100;
npay=basic+da+hra;
gross=npay-pf;
System.out.println(“Name of the Employee=“+st);
System.out.println(“The net pay = “+npay);
System.out.println(“The gross pay=“+gross);
}
}

You might also like