What is Scanner Class in Java

Scanner is a class in java.util bundle utilized for getting the contribution of the crude kinds like int, twofold, and so on and strings. It is the simplest method for perusing input in a Java program, however not extremely proficient assuming that you need an info technique for situations where time is a limitation like in cutthroat programming.

In the event that you are composing a Java program and wish to peruse the contribution from the client, you utilize Scanner Class in Java. In this article, I will give you a short understanding into Scanner class and its different techniques. In this article, I will cover the beneath referenced subjects:

What is the Scanner class?

The Scanner class is essentially used to get the client info, and it has a place with the java.util bundle. To utilize the Scanner class, you can make an object of the class and utilize any of the Scanner class techniques. In the underneath model, I am utilizing the nextLine() technique, which is utilized to understand Strings.

  • To make an object of Scanner class, we normally pass the predefined object System.in, which addresses the standard info stream. We might pass an object of class File to peruse input from a record.
  • To peruse mathematical upsides of a specific information type XYZ, the capacity to utilize is nextXYZ(). For instance, to peruse a worth of type short, we can utilize nextShort()
  • To understand strings, we use nextLine().
  • To peruse a solitary person, we use next().charAt(0). next() work returns the following token/word in the contribution as a string and charAt(0) work returns the primary person in that string.

Allow us to check out the code piece to peruse information of different information types.

// Java program to read data of various types using Scanner class.
import java.util.Scanner;
public class ScannerDemo1
{
    public static void main(String[] args)
    {
        // Declare the object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(System.in);
 
        // String input
        String name = sc.nextLine();
 
        // Character input
        char gender = sc.next().charAt(0);
 
        // Numerical data input
        // byte, short and float can be read
        // using similar-named functions.
        int age = sc.nextInt();
        long mobileNo = sc.nextLong();
        double cgpa = sc.nextDouble();
 
        // Print the values to check if the input was correctly obtained.
        System.out.println("Name: "+name);
        System.out.println("Gender: "+gender);
        System.out.println("Age: "+age);
        System.out.println("Mobile Number: "+mobileNo);
        System.out.println("CGPA: "+cgpa);
    }
}

Input:

eevibes
F
40
9876543210
9.9

Output:

Name: eevibes
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9

In some cases, we need to check assuming the following worth we read is of a particular sort or on the other hand in the event that the information has finished (EOF marker experienced).

Then, at that point, we check assuming the scanner’s feedback is of the kind we need with the assistance of hasNextXYZ() capacities where XYZ is the sort we are keen on. The capacity returns valid assuming the scanner has a badge of that sort, in any case bogus. For instance, in the underneath code, we have utilized hasNextInt(). To check for a string, we use hasNextLine(). Likewise, to check for a solitary person, we use hasNext().charAt(0).

Allow us to take a gander at the code bit to peruse a few numbers from control center and print their mean.

// Java program to read some values using Scanner
// class and print their mean.
import java.util.Scanner;
 
public class ScannerDemo2
{
    public static void main(String[] args)
    {
        // Declare an object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(System.in);
 
        // Initialize sum and count of input elements
        int sum = 0, count = 0;
 
        // Check if an int value is available
        while (sc.hasNextInt())
        {
            // Read an int value
            int num = sc.nextInt();
            sum += num;
            count++;
        }
        int mean = sum / count;
        System.out.println("Mean: " + mean);
    }
}

Input:

101
223
238
892
99
500
728

Output:

Mean: 397

This article is contributed by Sukrit Bhatnagar. In the event that you like eevibes and might want to contribute, you can likewise compose an article and mail your article to [email protected] See your article showing up on the eevibes primary page and help different eevibes.

If it’s not too much trouble, record pieces of feedback assuming that you find anything mistaken, or you need to share more data about the point examined previously

Also ReadHow to set checkbox size in HTML/CSS?

Leave a Reply

Your email address will not be published.