Scanner Class Java Presentation
Scanner Class Java Presentation
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;
• 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;
• 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.