Introduction
In Java programming, obtaining input from the user is a fundamental task that forms the basis for interactive applications. Whether you’re developing a simple console-based program or a sophisticated GUI application, user input is crucial for dynamic and responsive behavior. Java provides several mechanisms to capture user input, each suited to different scenarios and requirements.
There are two ways by which we can take Java input from the user or from a file
- Scanner Class
- Buffered Class Reader
Must read- Iteration Statements in Java, and Duck Number in Java.
1. Java Scanner Class
The Scanner class is the most widely used input method in Java. The Scanner class is used to read the input of primitive types such as int, double, long, etc., and some non-primitive types such as String, Boolean, etc.
The input is divided into tokens using a delimiter(which is whitespace by default) by the Scanner class. Methods like nextInt(), next(), nextLine(), nextFloat(), nextBoolean(), etc are used to scan the next token of the input.
The Java Scanner class is present in the Java.util package and has to be imported before using it. It extends the Object class and implements Iterator and Closeable interfaces.
You can also read about the topic of Java Destructor and Hashcode Method in Java.
Java Scanner Class Methods
Some of the methods available in the Java Scanner class are:
Method | Description | Example |
---|---|---|
nextInt() | Reads the next token of input as an int. | int num = scanner.nextInt(); |
nextLong() | Reads the next token of input as a long. | long num = scanner.nextLong(); |
nextFloat() | Reads the next token of input as a float. | float num = scanner.nextFloat(); |
nextDouble() | Reads the next token of input as a double. | double num = scanner.nextDouble(); |
nextBoolean() | Reads the next token of input as a boolean. | boolean val = scanner.nextBoolean(); |
nextLine() | Reads the next line of input as a String. | String line = scanner.nextLine(); |
next() | Reads the next complete token from the input as a String. | String word = scanner.next(); |
nextByte() | Reads the next token of input as a byte. | byte num = scanner.nextByte(); |
nextShort() | Reads the next token of input as a short. | short num = scanner.nextShort(); |
hasNextInt() | Checks if the next token of input can be interpreted as an int. Returns true if it can, otherwise false. | if(scanner.hasNextInt()) { ... } |
hasNextLong() | Checks if the next token of input can be interpreted as a long. Returns true if it can, otherwise false. | if(scanner.hasNextLong()) { ... } |
hasNextFloat() | Checks if the next token of input can be interpreted as a float. Returns true if it can, otherwise false. | if(scanner.hasNextFloat()) { ... } |
hasNextDouble() | Checks if the next token of input can be interpreted as a double. Returns true if it can, otherwise false. | if(scanner.hasNextDouble()) { ... } |
hasNextBoolean() | Checks if the next token of input can be interpreted as a boolean. Returns true if it can, otherwise false. | if(scanner.hasNextBoolean()) { ... } |
hasNextLine() | Checks if there is another line in the input of this scanner. Returns true if another line of input exists, otherwise false. | if(scanner.hasNextLine()) { ... } |
Example of Java Scanner Class:
Output:
Input
Method
in
Java
Advantages of Scanner Class
- It parses the user input and reads it in the desired data type.
- Each word in a string is obtained as a token and handled separately, making string manipulation easier.
- Easier to read data from files.
Disadvantages of Scanner Class
- Buffer size is less (1KB char buffer)
- Not safe for multithreaded programs
- There is ambiguity related to the nextLine() method as Scanner skips nextLine() after the use of any other next method like nextInt().