Scanner class java

747 views 7 slides Jun 16, 2021
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

Scanner class


Slide Content

1
JAVA Programming :
Scannerclass
Handled By
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College Perundurai
Dr.T.Abirami/Department of IT

User Input
2
TheScannerclass is used to get user
input, and it is found in
thejava.utilpackage.
Dr.T.Abirami/Department of IT

Scanner class Methods
3
Method Description
nextBoolean()Reads abooleanvalue from the user
nextByte() Reads abytevalue from the user
nextDouble()Reads adoublevalue from the user
nextFloat() Reads afloatvalue from the user
nextInt() Reads aintvalue from the user
nextLine()
next()
Reads aStringvalue from the user
nextLong() Reads alongvalue from the user
nextShort() Reads ashortvalue from the user
Dr.T.Abirami/Department of IT

Example:
4
Class sample
{
Public static
Dr.T.Abirami/Department of IT

Strings
TheStringdata type is used to store a
sequence of characters (text).
String values must be surrounded by
double quotes:
Example
String greeting = "Hello World";
System.out.println(greeting);
5Dr.T.Abirami/Department of IT

Example:
6
Class sample
{
Public static
Dr.T.Abirami/Department of IT

import java.util.Scanner;
class GetInputData
{
public static void main(String args[])
{
intnum;
float fnum;
String str;
Scanner in = new Scanner(System.in);
//Get input String
System.out.println("Enter a string: ");
str= in.nextLine();
System.out.println("Input String is: "+str);
//Get input Integer
System.out.println("Enter an integer: ");
num = in.nextInt();
System.out.println("Input Integer is: "+num);
//Get input float number
System.out.println("Enter a float number: ");
fnum= in.nextFloat();
System.out.println("Input Float number is: "+fnum);
}
}
7Dr.T.Abirami/Department of IT