If you’re starting with Java, the Scanner class is a great tool to help you read user input. In this article, we’ll show you how to use it and some of its most common features.
In this post, we’ll engage in a detailed discussion of the Scanner class in Java and its different methods. So, if you want to know more about the Scanner class in Java, keep reading until the end!
Post Type: | Core Java Tutorial, Java Programs Tutorial |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
What is the Scanner class in Java?
The Java Scanner class is mostly used to get input from the user. It’s located in the java.util package. The Scanner class not only extends the Object class but can also implement Iterator and Closeable interfaces. By default, it breaks up the user input into tokens using whitespace as a delimiter.
How do you use the Scanner class in Java?
You must import the Java to use the Scanner class in Java.util package. This also applies to other classes in Java – if you want to use a certain class, make sure to import the right package first. So, To use the Scanner class, you first need to import it using the import statement as shown below.
import java.util.Scanner;
How to Create Java Scanner Class Object?
To create an object of the Scanner class in java, you need to pass System.in into the constructor of the Scanner class. The system is a class in Java, a static variable of type InputStream. System.in represents the standard input stream. Here is the code snippet for the same:
Scanner input = new Scanner(System.in);
Here, the variable ‘input’ represents an object of the Scanner class. The System.in object is passed to initialize the ‘input’ object, representing the standard input stream. You can use this Scanner object to call various functions the Scanner class provides.
Note: In the above statement, we have taken the help of the user input stream to read the data, but If you want to read data from a file, you can pass an object of the “File” class into the Scanner class constructor. Most of the constructors use the following objects:
- InputStream is the most common, where we pass System.in to receive user input.
- File or Path – We can scan file data and work with the values from the file.
- String – We can create a scanner for a string source and parse its values.
Here is an example of using the above objects with the scanner class.
// read input from the input stream Scanner sc1 = new Scanner(InputStream input); // read input from files Scanner sc2 = new Scanner(File file); // read input from a string Scanner sc3 = new Scanner(String str);
Scanner Class Methods in Java
Let’s discuss some important Scanner class methods in Java necessary for taking input.
Here are some of the important methods of Scanner Class Methods in Java:
Method | Description |
---|---|
boolean nextBoolean() | By using this method, the user can read the boolean value. |
byte nextByte() | By using this method, the user can read the byte value. |
double nextDouble() | In this method, the user can accept the input in double datatype. |
float nextFloat() | By using this method, the user can take the float value. |
int nextInt() | Using this method, the user can scan the input in the int type. |
String nextLine() | By using this method, the user can read the String value. |
long nextLong() | Using this method, the user can read the long type of value. |
short nextShort() | Using this method, the user can read the short type of value. |
char next() | By using this method, the user can read the character input value. |
Apart from these scanner class java methods, there are a few other methods also available, like below:
Method | Description |
---|---|
hasNext() | It returns true if Scanner has another token for input or to mark whether the input is ended or not. If the user wants to know whether the input has been ended, then he can use this function. |
hasNextInt() | It checks whether the next token or input is of int data type. If that token is of int type, it will return true or false. |
hasNextFloat() | It checks if the next token or input is of float data type. If that token is float type, it will return true or false. |
hasNextDouble() | It checks if the next token or input is of double data type. If that token is of double type, it will return true or false. |
hasNextLine() | It is used to check whether the input has another line or string in the input. |
hasNextLong() | It checks whether the next token or input is of a long data type. If that token is of long type, it will return true or false. |
hasNextShort() | It checks whether the next token or input is of short data type. If that token is of long type, it will return true or false. |
hasNextByte() | It is used to check whether the next token or input is a byte. If that token is of byte type, it will return true or false. |
Scanner Class Program in Java Example
In this example of the scanner class in a Java program, we will see how to use the scanner class methods in Java programs.
package com.softwaretestingo.basic; import java.util.Scanner; public class ScannerClassEx1 { public static void main(String[] args) { Scanner sc = new Scanner(System. in ); System.out.println("Taking inputs from user"); System.out.println("Enter name of the Employee:"); // String input String name = sc.nextLine(); System.out.println("Enter post:"); String post = sc.nextLine(); System.out.println("Enter gender:"); // Character input char gender = sc.next().charAt(0); // Numerical data input System.out.println("Enter age:"); int age = sc.nextInt(); System.out.println("Enter mobile number:"); long mobileNo = sc.nextLong(); System.out.println("Enter salary"); double salary = sc.nextDouble(); // Print the values to check if the input was correctly obtained. System.out.println("\nName: " + name); System.out.println("Post: " + post); System.out.println("Gender: " + gender); System.out.println("Age: " + age); System.out.println("Mobile Number: " + mobileNo); System.out.println("Salary: " + salary); } }
Output:
Taking inputs from user Enter name of the Employee: SoftwareTestingo Enter post: QA Enter gender: Male Enter age: 25 Enter mobile number: 1234567890 Enter salary 55555 Name: SoftwareTestingo Post: QA Gender: M Age: 25 Mobile Number: 1234567890 Salary: 55555.0
Conclusion:
Great job on making it to the end of this article! The Scanner class is a very important part of taking user input. It allows you to take in all primitive data and String types. This makes it an incredibly versatile tool for Java programming.
If you take the time to learn about the Scanner class in Java and dedicate some time to practicing using it, you will slowly but surely become good at implementing it into your programs.