Scanner Class
Tags: beginners, java, discuss, computerscience
Published: 2021-06-20 08:59:26

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.

## Let's begin...
### 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.
![scanner.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1624168986657/2YCKkYve_.jpeg)
>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:-**
```Java
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](https://dev.to/ritvikdubey27/you-must-know-this-about-java-ii-1k21)***.

**Now we will create an object of Scanner to take input from console:-**
```Java
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:-**
```Java
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.
```Java
byte numberOfBooks = sc.nextByte();
```
- **nextShort() -** This method is used to take short type input from console.
```Java
short  numberOfUnits = sc.nextShort();
```
- **nextInt() -** This method is used to take integer type input from console.
```Java
int  numberOfChapters = sc.nextInt();
```
- **nextLong() -** This method is used to take long type input from console.
```Java
long  numberOfPages = sc.nextLong();
```
- **nextFloat() -** This method is used to take float type input from console.
```Java
float  pricePerUnit = sc.nextFloat();
```
- **nextDouble() -** This method is used to take double type input from console.
```Java
double  pricePerPage = sc.nextDouble();
```
- **nextBoolean() -** This method is used to take boolean type input from console.
```Java
boolean  purchasedBook = sc.nextBoolean();
```
- **next() -** This method is used to take String type input without space from console.
```Java
String  bookName = sc.next();
```
- **nextLine() -** This method is used to take String type input with space from console.
```Java
String  authorFullName = sc.nextLine();
```
>String is a class in Java, in future articles we will learn more about it.

![java.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1624178314252/cjCvNty7N.gif)
### Okay so that's enough for now follow my this journey to learn more about Java.
#### *Thank you for reading.*
#### *Please share your thoughts about it and correct me if I'm wrong.*
#### *I hope you liked it and found it helpful.*
#### ***Cover:- [Rajat Gour](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*