If you’re just getting started 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 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 Object class, but it can also implement Iterator and Closeable interfaces. By default, it breaks up the user input into tokens using whitespace as a delimiter.
How to use Scanner class in Java?
If you want to use the Scanner class in Java, you need to import the 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?
In order to create an object of the Scanner class in java, you need to pass System.in into the constructor of the Scanner class. System is a class in Java and in is a static variable of type InputStream . System.in represents the standard input stream. Here is code snippet for 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, which represents standard input stream. You can use this Scanner object to call various functions provided by the Scanner class.
Note: In the above statement we have taken help of 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 constructor using the following objects:
- InputStream – the most common where we pass System.in to receive user input.
- File or Path – We can scan file data too and work with the values from the file.
- String – We can create a scanner for a string source too and parse values from it.
Here is the example of how to use the above objects with 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 methods of the Scanner Class Methods in Java that are necessary for taking input.
Here are the some of the important methods of Scanner Class Methods in Java:
Method | Description |
---|---|
boolean nextBoolean() | By using this method user can reads the boolean value. |
byte nextByte() | By using this method user can reads the byte value. |
double nextDouble() | By using this method user can accepts the input in double datatype. |
float nextFloat() | By using this method user can takes the float value. |
int nextInt() | By using this method user can scans the input in the int type. |
String nextLine() | By using this method user can reads the String value. |
long nextLong() | By using this method user can reads the long type of value. |
short nextShort() | By using this method user can reads the short type of value. |
char next() | By using this method user can reads the character input value. |
Apart from these scanner class java methods there are few other methods are 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 input has been ended or not then he can use this function. |
hasNextInt() | It is used to check if the next token or input is of int data type or not. If that token is of int type, it will return true else false. |
hasNextFloat() | It is used to check if the next token or input is of float data type or not. If that token is of float type, it will return true else false. |
hasNextDouble() | It is used to check if the next token or input is of double data type or not. If that token is of double type, it will return true else false. |
hasNextLine() | It is used to check if there is another line or string in the input or not. |
hasNextLong() | It is used to check if the next token or input is of a long data type or not. If that token is of long type, it will return true else false. |
hasNextShort() | It is used to check if the next token or input is of short data type or not. If that token is of long type, it will return true else false. |
hasNextByte() | It is used to check if the next token or input is a byte or not. If that token is of byte type, it will return true else false. |
Scanner Class Program in Java Example
In this scanner class in java program example 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 inputs from the user. It allows you to take in all types of primitive data, as well as String types. This makes it an incredibly versatile tool for Java programming.
If you decide to 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.
Leave a Reply