📒 Unit III Notes: Input Using Scanner
Class (Java)
1. Introduction
In previous units, we learned:
● Using assignment statements
● Using output statements ([Link]())
Now we learn a better way to take input from the user at runtime using:
✅ Scanner Class
2. Scanner Class
✅ Definition:
Scanner class is a built-in Java class that allows the user to input values during program
execution.
It is defined inside the package:
[Link]
Scanner is useful for inputting:
● Integer values
● Floating values
● Double values
● String values
3. Importing the System Package
To use Scanner class, we must import:
import [Link].*;
Breakdown:
● import → keyword
● [Link] → package
● * → imports all classes
● ; → statement ends
4. Creating Scanner Object
Before using Scanner, we must create an object.
Syntax:
Scanner sc = new Scanner([Link]);
Meaning:
● Scanner → class name
● sc → object name
● new → allocates memory
● [Link] → takes input from keyboard
5. Functions to Input Data Using Scanner
(a) nextInt()
Used to input an integer.
Syntax:
int x = [Link]();
Example:
Scanner sc = new Scanner([Link]);
int p = [Link]();
(b) nextFloat()
Used to input a float value.
Syntax:
float x = [Link]();
Example:
float p = [Link]();
(c) nextLong()
Used to input a long integer.
Syntax:
long x = [Link]();
(d) nextDouble()
Used to input a double value.
Syntax:
double x = [Link]();
Note:
Functions for string input will be discussed later.
6. Mathematical Library Functions in
Java
Java provides many ready-made functions inside:
Math
These functions help perform numeric calculations easily.
(a) [Link]()
Returns the maximum of two numbers.
Syntax:
[Link](a, b)
Example:
int m = [Link](21, 89);
Output: 89
(b) [Link]()
Returns the minimum of two numbers.
Syntax:
[Link](a, b)
Example:
double n = [Link](25.7, 2.5);
Output: 2.5
(c) [Link]()
Returns power value.
Syntax:
[Link](base, power)
Example:
double d = [Link](3, 2);
Output: 9.0
(d) [Link]()
Returns square root of a positive number.
Syntax:
[Link](number)
Example:
double n = [Link](25);
Output: 5.0
Note:
Square root of a negative number cannot be determined.
7. Solved Java Programs Using Scanner
Class
Program 1: Employee Salary Calculation
Given:
● DA = 30% of Basic Pay
● HRA = 20% of Basic Pay
● PF = 12% of Basic Pay
Formula:
Gross Pay = Basic + DA + HRA
Net Pay = Gross Pay - PF
Program 2: Greatest of Three Numbers
Input three numbers and display greatest.
Logic:
if(a>b && a>c)
greatest = a;
else if(b>a && b>c)
greatest = b;
else
greatest = c;
Program 3: Final Velocity of Vehicle
Formula:
[
v = \sqrt{u^2 + 2as}
]
Where:
● u = initial velocity
● a = acceleration
● s = distance
Uses:
[Link]()
[Link]()
Program 4: Perfect Square Number Check
A number is a perfect square if:
[
\sqrt{n} \times \sqrt{n} = n
]
Example:
✅ perfect square
❌
● 81 → √81 = 9 → 9×9 = 81
● 92 → not perfect square
8. Chapter at a Glance (Quick Revision)
● Scanner class is used for taking user input.
● Import required:
import [Link].*;
● Scanner object creation:
Scanner sc = new Scanner([Link]);
● Input functions:
Function Used
for
nextInt() integer
nextFloat() float
nextLong() long
nextDouble() double
●
Math functions:
Function Meaning
[Link]() maximum
[Link]() minimum
[Link]() power
[Link]() square
root