Last Updated on January 2, 2023 by Prepbytes
In this article, we will learn about what is a scanner in java, and what are different methods of scanner classes are, and we will learn to write a java scanner program, and working of a scanner class in java.
What is scanner class in java?
A scanner class in java is mainly used to take input from the users, streams, files, etc. The java.util package contains a scanner class. We can use a scanner class to take various types of inputs from a user. Let’s understand a scanner class using one java scanner program.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number: "); int number= sc.nextInt(); System.out.println("The entered number is: "+number); } }
Output
Enter the number: 100
The entered number is: 100
In the above program, we write one syntax import java.util.Scanner; which means we are importing a Scanner class from the package java.util.After that, we created an object of a Scanner class and the syntax is Scanner sc = new Scanner(System.in); in this syntax System.in is used to take input from the keyboard. After that, we take integer input from the user using the method nextInt(). In the output, we can see that first we took input from the user and after that, we print that input.
Methods of Java Scanner to take input:
Method | Description |
---|---|
nextInt() | This method is used to take input of int data type. |
nextFloat() | This method is used to take input of float data type. |
nextDouble() | This method is used to take input of double data type. |
nextBoolean() | This method is used to take input of boolean data type. |
nextLine() | This method is used to take line of text input. |
next() | This method is used to take input as a word. |
nextByte() | This method is used to take input of byte data type. |
nextShort() | This method is used to take input of short data type. |
nextLong() | This method is used to take input of long data type. |
We will see some examples of the java scanner program to understand how to use the above method to take input from the user.
Java Scanner nextInt() method:
We can use this method to take input of int data type.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the age: "); int number= sc.nextInt(); System.out.println("The age is: "+number); } }
Output
Enter the age: 22
The age is: 22
In the above java scanner program, first, we have created an object of the scanner class. After that, we used the nextInt() method to take int input from the user. If we will give any other input apart from the int program will throw an error that java.util.InputMismatchException. In the output, we can see that the input number is printed.
Java Scanner nextFloat() method and nextDouble() method:
Both the above methods are used to take a floating-point number from the user. The only difference is the size, float stores 4 bytes of data while double stores 8 bytes of data.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the salary: "); float salary= sc.nextFloat(); System.out.println("The age is: "+salary); System.out.print("Enter the discount: "); double discount= sc.nextDouble(); System.out.println("The age is: "+discount); } }
Output
Enter the salary: 2351.014
The age is: 2351.014
Enter the discount: 26.154326356945
The age is: 26.154326356945
In the above java scanner program, first, we have created an object of the scanner class. After that, we used the nextFloat() method to take the first float input from the user and we used the nextDouble() method to take the second double input from the user. If we will give any other input apart from the float and double program will throw an error. In the output, we can see that the input number is printed.
Java Scanner next() method:
This method is used to take input as a word from the user.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the course name: "); String course= sc.next(); System.out.println("The course name is: "+course); } }
Output
Enter the course name: computer science
The course name is: computer
In the above java scanner program, first, we have created an object of the scanner class. After that, we used the next() method to take input as a word from the user. If we will give any other input apart from the int program will throw an error. In the input we have given “computer science” but in the output, only computer is printed because the next() method will take only one word it will stop taking characters if it gets whitespace.
Java Scanner nextLine() method:
This method is used to take input as a line of text.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the course name: "); String course= sc.nextLine(); System.out.println("The course name is: "+course); } }
Output
Enter the course name: computer science
The course name is: computer science
In the above java scanner program, first, we have created an object of the scanner class. After that, we used the nextLine() method to take input as a line of text from the user. If we will give any other input apart from the int program will throw an error. In the input, we have given “computer science”. In the previous method next(), we will get only “computer” as an output but in this method, we will get the whole line “computer science” because the nextLine() method takes input as a line of text.
Java Scanner nextBoolean() method:
This method is used to take boolean input from the user.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Are you a student?: "); boolean is_student= sc.nextBoolean(); if(is_student){ System.out.println("The user is a student"); } else{ System.out.println("The user is not a student"); } } }
Output
Are you a student?: false
The user is not a student
In the above java scanner program, first, we have created an object of the scanner class. After that, we used the nextBoolean() method to take boolean input from the user. If we will give any other input apart from the int program will throw an error. We gave false as an input so in the output, we can see that “The user is not a student” is printed. If we gave true as input then “The user is a student” will be printed.
Java Scanner nextBigInteger() and nextBigDecimal() method:
BigInteger() is used to take input as a big integer and BigDecimal() is used to take input as a big decimal. BigInteger and BigDecimal are not primitive data types thus we have to import classes for both.
import java.util.Scanner; import java.math.BigInteger; import java.math.BigDecimal; class PrepBytes { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number: "); BigInteger number=sc.nextBigInteger(); System.out.println("The number is: "+number); System.out.print("Enter the decimal number: "); BigDecimal decimal_number=sc.nextBigDecimal(); System.out.println("The number is: "+decimal_number); } }
Output
Enter the number: 123456789
The number is: 123456789
Enter the decimal number: 12.123465679
The number is: 12.123465679
In the above java scanner program, first, we have imported classes of BigInteger and BigDecimal from the package java.math. After that, we created an object of the scanner class. After that, we used the nextBigInteger() method to take the first big integer input from the user and we used the nextDecimalInteger() method to take the second big decimal input from the user. If we will give any other input apart from the float and double program will throw an error. In the output, we can see that both the big integer and the big decimal number are printed.
Working of Java Scanner class:
When we give any input, the Scanner class first read an entire line. After that, the Scanner class divides that line into tokens. To divide a line into tokens the Scanner class uses whitespace. Let’s take an example to understand this.
Example: The salary is 50000
Considering the above example, the Scanner class first read an entire input line. After that, a line is divided into tokens like “The”, “salary”, “is”, and “50000”. After that, the object of the scanner class iterates through all the tokens and read them using the various methods we see above.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java
String Palindrome Program in Java
Thread Program in JAVA