Introduction

Have you ever wondered how the website reads such complex and varying user input? Well, it is done with the help of a Scanner class. Suppose you’re on a website to book your dream holiday. After choosing the departure and return date, you are asked to fill in your details, such as your name, age, address, phone numbers, and other vital information. Each of these inputs varies significantly in the type of data entered — Name – short strings with spaces, age and contact numbers – numbers, email id – strings without spaces.

Scanner class, as the name suggests, is used to scan. But “Scan what?” you may ask. If you are writing a Java program and want to read the user’s input, you make use of the Scanner class input in Java. We can define Scanner Class in Java as a class that scans the user’s information (keystrokes) and reads it. The input can then be used for determining the output of the Java program. Let us explain the scanner class in Java more in detail.

  1. What is the Scanner Class in Java?
  2. What is the Use of Scanner Class in Java?
  3. How to Use Scanner class in Java?
  4. What are Scanner Class Methods in Java?

1. What is the Scanner Class in Java?

The Scanner class in Java is a class included in the Java.util package for Scanner class in Java. It is used to obtain various input types such as int, long, byte, double, and strings. Most Java programs rely on user input and using Scanner class results in being the easiest method to get user input. However, the use of Scanner class in Java becomes inefficient if used in a constraining environment such as competitive programming. Now that you’ve understood what is Scanner class in Java, let’s have a look at the use of Scanner class in Java.

2. What is the use of Scanner class in Java?

As mentioned earlier, the reason why we use Scanner class in Java is used to obtain user input value. The function of the scanner class in Java is to parse text for strings and primitive types using a regular expression. Here’s how you can declare a Scanner class in the Java package:

public final class Scanner  

extends Object  

implements Iterator<String>   

3. How to use Scanner Class in Java?

To use the Scanner class in Java, you need to first include the Scanner class in the Java program. Here’s how to import Scanner class in Java with the Java.util package with the import keyword.

For example:

import.Java.util.Scanner

This command is used to import Scanner class in Java. Similarly, we can use

import.Java.util.*

This command is used to import all the classes of the Java.util package.

Importing the Scanner class is the first step in using Scanner class in Java, then we need to instantiate a Scanner object. This can be done by creating a variable of type Scanner.

Scanner class in Java Example

public static void main(String [ ] args)

{

Scanner input = new Scanner(System.in);

}

In the above example, the input is the variable that is going to be provided by the user. The variable is assigned to a new Scanner, also known as the Scanner class construct in Java. Thus we are creating an object (Scanner input) that reads data from System.in(user input).

The above example of Scanner class program in Java can be further simplified as, 

 public static void main(String [ ] args)

{

Scanner input = new Scanner(“Jigsaw Academy”);

}

The string “Jigsaw Academy” will be entered by the user which will be read by the Scanner class.

4. What are Scanner Class Methods in Java?

There are various Scanner class methods in Java to receive user input. These methods are used to read specific user input data from the keyboard. The Scanner class methods in Java are discussed in the table below.

nextLine()Reads a string value from the user.
nextInt()Reads an integer from the user.
nextDouble()Reads a double value from the user.
nextBoolean()Reads a boolean value from the user.
nextShort()Reads a short value from the user.
nextLong()Reads a long value from the user.
nextFloat()Reads a float value from the user.
nextByte()Reads a byte value from the user.

When any of the above methods are called in a Java program, the program’s execution is paused until the user inputs a value. These methods return the entered value.

Scanner class in Java examples

Let’s have a look at a few examples to understand how to use Scanner class in Java.

  • Factorial program in Java using Scanner class

import Java.util.Scanner;

class Factorial

{

public static void main(String args[])

{

int i, n, f = 1;

System.out.printIn(“Enter an integer”);

Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n<0)

System.out.printIn(“Please enter a positive integer”);

else

{

for (i = 1; i <=n ; i++)

f =f*i;

System.out.printIn (“The answer is” = =f);

}

}

}

  • Prime number program in Java using Scanner class

import Java.util.Scanner;

public class PrimeNumber

{

             public static void main(String args[])

          {

               int num,b,c;

               Scanner s=new Scanner(System.in);

               System.out.println(“Enter A Number”);

               num =s.nextInt();

               b=1;

               c=0;

                while(b<= num)

                   {

                       if((num%b)==0)

                         c=c+1;

                          b++;

                   }

                    if(c==2)

                    System.out.println(num +” is a prime number”);

                    else

                    System.out.println(num +” is not a prime number”);

          }

}

  • Java program to read data of various types using Scanner class. 

import Java.util.Scanner; 

public class ScannerDemo 

public static void main(String[] args) 

Scanner sc = new Scanner(System.in); 

String name = sc.nextLine(); 

char gender = sc.next().charAt(0); 

int age = sc.nextInt(); 

long mobileNo = sc.nextLong(); 

double cgpa = sc.nextDouble(); 

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); 

Conclusion

Key points of Scanner class in Java.

  • Scanner class in Java is used to scan/read user input
  • The Scanner class in Java is a class contained in the Java.util package.
  • Depending upon the input, various Scanner class input methods can be used.
  • nextLine() and nextInt() are the most commonly used input methods. 
  • The nextLine is used to read input from the input device till the line changes.
  • nextInt() is used to input the value of an integer variable ‘n’ from the user.

To know more about Software development through Java, check out our website!

Also Read

SHARE
share

Are you ready to build your own career?