0% found this document useful (0 votes)
15 views8 pages

Lecture - 3.4-Scanner Class

The document discusses taking input from the user using the Scanner class in Java. It explains that Scanner is used to read input from System.in and lists commonly used methods like nextInt() and nextLine(). It provides examples of taking input like numbers and strings and calculating sum or displaying user information.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Lecture - 3.4-Scanner Class

The document discusses taking input from the user using the Scanner class in Java. It explains that Scanner is used to read input from System.in and lists commonly used methods like nextInt() and nextLine(). It provides examples of taking input like numbers and strings and calculating sum or displaying user information.

Uploaded by

d.vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lecture – 3.

4
JAVA: Scanner Class

1
Today’s Topics
• Taking input using Scanner class
• Some exercises using scanner class
Taking input from User
Scanner Class
• ‘Scanner’ class is used to take input from user

• An object of Scanner class is created to read input

• Java uses System.out to refer to the standard output device and


System.in to standard input device

• Import ‘java.util.Scanner’ package to use scanner class


Commonly used methods for Scanner class

• nextByte(): reads an Byte value from the user


• nextShort() : reads an Short value from the user
• nextInt() : reads an int value from the user
• nextLong() : reads an Long Integer value from the user
• nextFloat() : reads an Float value from the user
• nextDouble() : reads an Double value from the user
• next() : reads a word from the user
• nextLine() : reads a Line of Text from the user
Exercise
Using Scanner class

• Take two integer numbers as input and calculate their sum.

• Calculate the area of a circle. Take radius as input.

• Take some information about yourself (i.e. name, age, cgpa, department,
section etc) as input and display them.
1.import java.util.*;
2.public class ScannerExample {
3.public static void main(String args[]){
4. Scanner in = new Scanner(System.in);
5. System.out.print("Enter your name: ");
6. String name = in.nextLine();
7. System.out.println("Name is: " + name);
8. }
9. }
Thank You

You might also like