What Does Scanner Do in Java?
Introduction
In Java, the Scanner class is a fundamental component of the Java Standard Input/Output (I/O) API. It is used to read input from the user or from a file and to perform various operations on the input data. In this article, we will explore what the Scanner class does in Java and provide examples of its usage.
What is Scanner?
A Scanner is an object that reads input from a source, such as the keyboard or a file. It is a part of the Java Standard Input/Output (I/O) API and is used to read input from the user or from a file. The Scanner class is a subclass of the InputMismatchException class, which is a subclass of the Exception class.
Methods of Scanner
The Scanner class has several methods that can be used to read input from the user or from a file. Here are some of the most commonly used methods:
next(): This method reads a single character from the input stream and returns it as a string.nextInt(): This method reads an integer from the input stream and returns it as an integer.nextDouble(): This method reads a double-precision floating-point number from the input stream and returns it as a double.nextLine(): This method reads a line of input from the input stream and returns it as a string.nextCharArray(): This method reads a character array from the input stream and returns it as a character array.nextBoolean(): This method reads a boolean value from the input stream and returns it as a boolean.nextByte(): This method reads a byte from the input stream and returns it as a byte.nextShort(): This method reads a short integer from the input stream and returns it as a short integer.nextLong(): This method reads a long integer from the input stream and returns it as a long integer.nextFloat(): This method reads a float from the input stream and returns it as a float.nextBigInteger(): This method reads a big integer from the input stream and returns it as a big integer.
Example Usage
Here is an example of how to use the Scanner class:
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your favorite color:");
String color = scanner.nextLine();
scanner.close();
}
}
In this example, we create a Scanner object and use its methods to read input from the user. We then use the nextLine() method to read a line of input from the user and store it in the name variable.
Error Handling
The Scanner class also provides error handling mechanisms to handle various types of errors that may occur while reading input from the user or from a file. Here are some of the error handling mechanisms provided by the Scanner class:
InputMismatchException: This exception is thrown when the Scanner object attempts to read input from the user that is not a valid character.IOException: This exception is thrown when an I/O error occurs while reading input from the user or from a file.NullPointerException: This exception is thrown when the Scanner object is used without initializing it.
Table: Scanner Class Methods
| Method | Description |
|---|---|
next() |
Reads a single character from the input stream and returns it as a string. |
nextInt() |
Reads an integer from the input stream and returns it as an integer. |
nextDouble() |
Reads a double-precision floating-point number from the input stream and returns it as a double. |
nextLine() |
Reads a line of input from the input stream and returns it as a string. |
nextCharArray() |
Reads a character array from the input stream and returns it as a character array. |
nextBoolean() |
Reads a boolean value from the input stream and returns it as a boolean. |
nextByte() |
Reads a byte from the input stream and returns it as a byte. |
nextShort() |
Reads a short integer from the input stream and returns it as a short integer. |
nextLong() |
Reads a long integer from the input stream and returns it as a long integer. |
nextFloat() |
Reads a float from the input stream and returns it as a float. |
nextBigInteger() |
Reads a big integer from the input stream and returns it as a big integer. |
Conclusion
In conclusion, the Scanner class is a fundamental component of the Java Standard Input/Output (I/O) API. It provides various methods to read input from the user or from a file and to perform various operations on the input data. The Scanner class also provides error handling mechanisms to handle various types of errors that may occur while reading input from the user or from a file. By understanding the methods and error handling mechanisms provided by the Scanner class, developers can write efficient and robust Java programs that can handle various types of input data.
