Part 4 Scanner Class 1725270023
Part 4 Scanner Class 1725270023
Part 4 Scanner Class 1725270023
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
Method Description
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
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.
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
double b= sc.nextDouble();
double h= sc5.nextDouble();
double area=(b*h)/2;
}*/
HOME ASSIGNMENT
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 );
}
}