Hello People, In this short read I'll be writing about the Scanner class in Java. We will learn about why we use it, why it is important and most important its methods.
What is Scanner?
Scanner as you hear the word scanner the first thing that may come to your mind will be a computer scanner or those mobile applications which you use for scanning purposes. So let's first understand what a computer scanner is and what it does.
A scanner is an input device. If the user needs to input some written information from a page, then the scanner helps the user to input data directly from the page to the computer system.
Similarly, in Java Scanner is a class found in the java.util package, there are many other ways to take input from the user present in this package, Scanner class is used to take input from keyboard line by line. Scanner can take input of any type and it splits the input after every whitespace. It is the simplest way to get input in Java..
How to use Scanner?
To use Scanner first you need to important it in your program. There are following two ways you can import it:-
1. import java.util.Scanner; 2. import java.util.*; //this is bad practice
To learn more about packages check my article You Must Know This About Java - II.
Now we will create an object of Scanner to take input from console:-
Scanner sc = new Scanner();
So, here we make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable sc. At the same time you are calling the constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program.
Scanner class accepts input stream as a parameter and System class have a static variable in which is of type InputStream. System.in gives you a instance of of type InputStream. So basically using System.in Scanner class becomes able to read the data from console.
Now we will take a specific type of value from it and store it in a variable:-
int marks = sc.nextInt();
So we have created a variable called sc that collects the next value the user inputs into the console. Then we created a variable called marks that collects the value the user submits to the console.
Now what is nextInt() here?
So as we have seen the complete structure above how Scanner class works there is this one big thing nextInt(), so this is a method which we use to take integer type input from console. There are several different types of methods used to take input from console. They are as follows:-
- nextByte() - This method is used to take byte type input from console.
byte numberOfBooks = sc.nextByte();
- nextShort() - This method is used to take short type input from console.
short numberOfUnits = sc.nextShort();
- nextInt() - This method is used to take integer type input from console.
int numberOfChapters = sc.nextInt();
- nextLong() - This method is used to take long type input from console.
long numberOfPages = sc.nextLong();
- nextFloat() - This method is used to take float type input from console.
float pricePerUnit = sc.nextFloat();
- nextDouble() - This method is used to take double type input from console.
double pricePerPage = sc.nextDouble();
- nextBoolean() - This method is used to take boolean type input from console.
boolean purchasedBook = sc.nextBoolean();
- next() - This method is used to take String type input without space from console.
String bookName = sc.next();
- nextLine() - This method is used to take String type input with space from console.
String authorFullName = sc.nextLine();
String is a class in Java, in future articles we will learn more about it.