Part 4 Scanner Class 1725270023

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

SCANNER

CLASS
IN
JAVA
❖ Define Scanner class
❖ Syntax of scanner class
❖ Different methods of Scanner class
❖ Example programs
❖ Recap of the topics taught
❖ Home assignment
Scanner is a class in java. util package used for obtaining the input of the primitive
types like int, double, etc. and strings.

Syntax:
import package. name. Class name; // Imports a single class
e.g., import java . util . Scanner ; //Only the Scanner class of
java . util package can
be used
class ScannerClassInput
{ …………………………….}
OR
import package. name.*; // Import the whole package
e.g., import java . util . * ; //all classes of java . util package
can be used
class ScannerClassInput
{ …………………………….}
Definition : The class that allows to input or read primitive data type (like
short, int ,float etc.) and String. It can be used to get input
from InputStream.

❑ The Scanner breaks input into tokens using white space as default
delimiter.
❑ The resulting tokens may then be converted into values of different
primitive type using various next methods.
❑ A token is a series of characters that ends with whitespace.
❑ A whitespace character can be a blank , tab character, carriage
return, or the end of the file.
INSTANTIATION OF SCANNER CLASS OBJECT

▪ Creating a Scanner class object is called instantiation.

▪ This can be done as follows:

E.g., Scanner sc = new Scanner ( System.in )

• new operator allocates memory for a Scanner object.

▪ In the above code, we are using the predefined System.in,


which is typically used in Java console programs to accept the
keyboard input and is one of several ways to read user input
from the keyboard.
 Use the following methods to accept primitive data types and
Strings.

Method Description

nextBoolean() Reads a boolean value from the user

nextByte() Reads a byte value from the user

nextShort() Reads a short value from the user

nextInt() Reads an int value from the user

nextLong() Reads a long value from the user

nextDouble() Reads a double value from the user

nextFloat() Reads a float value from the user

next() Reads a word from the user

nextLine() Reads multiple words from the user


 Use next () method to read one word

 using Scanner class object name

Example:
import java.util.Scanner;
class MyClass
{ public static void main ( String args [ ] )
{ Scanner sc = new Scanner ( System.in );
System.out.println ( “ Enter first name “ );
String name = sc.next();
System.out.println ( “ First name is: " + name );
}
}
 Use nextLine () method to read multiple word

✓ using Scanner class object name

Example:
import java.util.Scanner;
class MyClass
{ public static void main ( )
{ Scanner sc = new Scanner ( System.in );
System.out.println ( “ Enter a sentence “ );
String str = sc.nextLine ();
System.out.println ( “ Given sentence : " + str );
}
}
Write a program to
accept user’s name ,
age and salary. Display
all the detail in a neat
format.
Write a program to accept user’s name , age and salary. Display all
the detail in a neat format.

import java.util.Scanner;
class MyClass
{ public static void main ( String[] args )
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
String name = sc.nextLine(); // String input
char ch = name. charAt(0);
int age = sc.nextInt(); // Numerical input
double salary = sc.nextDouble();
System.out.println("Name : " + name);
System.out.println("First character of the name : "+ ch);
System.out.println("Age : " + age);
System.out.println("Salary : " + salary);
} }
Write a program to accept base and
height of a triangle. Calculate the area
of a triangle. Display all the detail in a
neat format.

Area = 1/2base x height


Write a program to accept radius of a sphere and
calculate volume of a sphere. Display all the details in
a neat format.

V= 𝟒 𝝅𝒓𝟑
𝟑
Write a program to accept base and
height of a triangle. Calculate the area
of a triangle. Display all the detail in a
neat format.
1
Area = bh
2
Write a program to accept base and height of a triangle. Calculate the area of a
triangle. Display all the detail in a neat format.
Area = 1/2base x height

import java.util.*;

class AreaOfTriangle

{ public static void main()

{ Scanner sc = new Scanner (System. in);

System.out.println(" Enter base and height of a Triangle:");

double b= sc.nextDouble();

double h= sc5.nextDouble();

double area=(b*h)/2;

System.out.println("Base and height of a Triangle are :" + b+ " \t" + h);

System.out.println(" Area of the triangle is :" +area );

}*/
HOME ASSIGNMENT

WAP to accept a two digit number. Calculate sum of the digits


and reverse the digits. Display all the details in a neat format.
E.g. Input = 27;
output : Sum :9
Reverse : 72
❖ Define Scanner class
❖ Syntax of scanner class
❖ Utility of import keyword
❖ Different methods of Scanner class
Write a program to accept radius of a sphere and calculate volume of a
sphere. Display all the details in a neat format.

V=4/3π r3

class Vol_sphere
{ public static void main ( String args [ ] )
{ double r = Double. parseDouble ( args[0] );
double vol = 4/3.0 * 22/7.0 * r * r * r ;
System.out.println(" Radius\t Volume of a cone ");
System.out.println ( r+ "\t“ + vol );

}
}

You might also like