0% found this document useful (0 votes)
2 views

Scanner Class Java Presentation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Scanner Class Java Presentation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to the Scanner Class

in Java
II-I/CSE-E
A.Anand /6 Sep
Overview of Scanner Class
• What is the Scanner Class?
• - A part of java.util package
• - Used to read input from sources like
keyboard
• Common Methods:
• - nextInt(), nextFloat(), nextLine(),
nextBoolean(), and more.
Basic Setup of Scanner
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• // Your code for taking inputs
• }
• }

• Explanation:
• - Scanner sc = new Scanner(System.in); creates a Scanner object to read
from the console.
commonly used input methods
provided by the Scanner class in Java:
• nextInt(): Reads an integer value.
• nextFloat(): Reads a floating-point number.
• nextDouble(): Reads a double-precision floating-point number.
• nextLong(): Reads a long integer.
• nextShort(): Reads a short integer.nextByte(): Reads a byte
value.next(): Reads a single word (delimited by space).
• nextLine(): Reads the entire line (including spaces).
• nextBoolean(): Reads a boolean value (true or false).
• nextBigInteger(): Reads a BigInteger value
(requires java.math.BigInteger).
• nextBigDecimal(): Reads a BigDecimal value
(requires java.math.BigDecimal).
Taking Integer Input
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• System.out.println("Enter an integer:");
• int num = sc.nextInt();
• System.out.println("You entered: " +
Taking Float Input
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• System.out.println("Enter a float
number:");
• float num = sc.nextFloat();
Taking String Input
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• System.out.println("Enter a string:");
• String str = sc.next();
• System.out.println("You entered: " + str);
Taking Full Line Input
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• System.out.println("Enter a full line:");
• String line = sc.nextLine();
• System.out.println("You entered: " + line);
Taking Boolean Input
• Code Example:
• import java.util.Scanner;

• public class Example {


• public static void main(String[] args) {
• Scanner sc = new Scanner(System.in);
• System.out.println("Enter true or false:");
• boolean bool = sc.nextBoolean();
• System.out.println("You entered: " +
Handling Input Mismatches
• Explanation:
• - If wrong input type is entered,
InputMismatchException is thrown.
• Example: If nextInt() is called and user enters a
string, an error occurs.
Closing the Scanner
• Importance of Closing Scanner:
• - Use sc.close() to free resources after input.

• Example:
• sc.close();
Conclusion
• Key Points:
• - Scanner class supports input types: int, float,
string, boolean, etc.
• - Handle exceptions and close the Scanner to
avoid resource leaks.

You might also like