Java Scanner Example
This is one of the articles from our Java Tutorial for Beginners.
We suggest that you recall two real-world examples which will help you understand the topic of this article.
- When we're about to go on a flight, the staff at the airport first check your luggage with a scanner machine. The machine scans your bag and the airport employee finds out exactly what you have in it.
- Scanners in stores work the same way. An employee at a check-out stand scans a bar code and sees all the information that is listed under that bar code.
There are similar tasks in the world of Java programming. For example, it's often necessary to carry out the following tasks:
- A user enters some number in the console and then the program has to read out the number which the user entered in the console.
- A user enters some word in the console and then the program has to read out the word which the user entered in the console.
To carry out these tasks, Java uses the scanner. Remember: if something is entered in the console and you want to read what was entered, then you need to use the scanner.
We will examine several examples of code, after which you will:
- understand how the scanner works in practice. There are 6 examples of code in this article. We recommend that you run all of them on your computer and learn how they work in practice.
- master four scanner methods:
next ();
nextLine ();
nextInt ();
nextDouble ();
Methods are, roughly speaking, the actions that the Scanner can perform. In fact, there are many scanner methods. But at this stage, it will be sufficient to know just 4. Let’s get started!
Example No. 1 – The nextInt () method
Let's suppose that we want the user to enter any integer from 1 to 10 in the console, and we want the program to display the number that the user entered.
Since we need to “scan” the number entered the user entered, we need to use the scanner. You can see the solution below, as well as comments on it.
Solution:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; class Test { public static void main(String args[]){ System.out.print("Enter any integer from 1 to 10: "); Scanner scan = new Scanner(System.in); int number = scan.nextInt(); System.out.println ("You entered " + number); } } |
If you run this code on your computer, you will see the following in the console:
Enter any integer from 1 to 10:
And if you enter, for example, the number 5, you will see the following in the console:
Enter any integer from 1 to 10: 5
You entered 5
Commentary:
In the article “What is the Java Class library?” we learned that Java has a huge library of tested code – ready-made frameworks for many problems that programmers face in their daily work. We also talked about various packages, classes, and methods. So, now we are going to work with the java.util package. This package includes the Scanner class, which has methods (actions) which allow us to work with the input and output of information in the console.
In order to be able to use the Scanner class, we first need to take 3 steps.
Step No. 1 – Write the following line in the code:
1 |
import java.util.Scanner; |
Why did we write this line? It helped us import the Scanner class from the java.util package. If we had forgotten to write it, the scanner simply wouldn’t work. Also, take note of where we wrote the line.
Then we asked the user to enter any whole number (integer) from 1 to 10. To do this, we wrote:
1 |
System.out.print("Enter any integer from 1 to 10: "); |
Step No. 2 – Declare the scanner
1 |
Scanner scan = new Scanner(System.in); |
- With the help of Scanner scan, we declared that the variable will be called “scan” and it will fall into the Scanner class. It’s necessary to write the word Scanner in front of the name of the variable. By the way, you can call it whatever you want; you don’t need to call it “scan.”
- Then we wrote = new Scanner (System.in);
When you get to the topic of classes, you will gain a greater understanding of this construction.
Step No. 3 – Read the number from the console
1 |
int number = scan.nextInt(); |
-
- With the help of int number, we declared the variable number of the int type. Why did we choose the int type for the variable? Because, according to the condition of the task, the user has to enter any integer from 1 to 10. And since we expect that the number entered by the user will be a whole number (an integer), we chose the int type for the variable "number."
- = scan. nextInt (); this method of the nextInt () scanner is responsible for reading out the whole number which the user entered in the console. With the help of scan.nextInt () we read the entered whole number from the console and assign it to the variable "number."
That’s it. From now on, the variable "number" will have the number entered by the user. Hurray!
Then we needed to enter the number that the user entered in the console and we did it with the help of this line:
1 |
System.out.println ("You entered " + number); |
Example No. 2 – The nextInt () method
Ask the user to enter any two whole numbers (integers). After that, you need to display the sum of these two numbers in the console.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner k = new Scanner(System.in); System.out.println ("Enter any two integers: "); int number1 = k.nextInt(); int number2 = k.nextInt(); System.out.print(number1 + number2);//it displays the sum of number1 + number2 } } |
If run this code on your computer, you will see the following in the console:
Enter any two integers:
And if you enter, for example, the numbers 2 and 3, you will see the following in the console:
Enter any two integers:
2
3
5
Commentary:
Again, it takes 3 steps to use the scanner.
Step No. 1 – Import the scanner from the java.util package:
1 |
import java.util.Scanner; |
Then we asked the user to enter two any integers:
1 |
System.out.println ("Enter two any integers: "); |
Step No. 2 – Declare the scanner
1 |
Scanner k = new Scanner(System.in); |
- With the help of Scanner k, we declared that the variable will be called “k” and will fall into the "Scanner" class. It’s necessary to write the word "Scanner" in front of the name of the variable. By the way, you can call it whatever you want; you don’t need to call it “k.”
- Then we wrote = new Scanner (System.in);
Step No. 3 – Read out the number from the console
Actually, we did this two times, because we asked the user to enter two different numbers:
1 |
int number1 = k.nextInt(); |
1 |
int number2 = k.nextInt(); |
That’s it. From now on, the variables "number1" and "number2" will have the two numbers that the user entered.
And then we displayed the sum of "number1" and "number2":
1 |
System.out.print(number1 + number2);//it displays the sum of number1 + number2 |
Example No. 3 – The nextLine () method
Let's suppose that we want the user to enter any word or phrase in the console and we want the program to display the word or phrase the user entered.
Solution:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any word or phrase: "); String phrase1 = sc.nextLine(); System.out.println(phrase1); } } |
If you run this code on your computer, you will see the following in the console:
Enter any word or phrase:
And if the user enters “I’m the Lord of the Scanner”, you will see the following in the console:
Enter any word or phrase:
I’m the Lord of the Scanner
I’m the Lord of the Scanner
Commentary:
Again, it takes 3 steps to use the scanner.
Step No. 1 – Import the scanner from the java.util package:
1 |
import java.util.Scanner; |
Then we asked the user to enter any word or phrase:
1 |
System.out.println("Enter any word or phrase: "); |
Step No. 2 – Declare the scanner
1 |
Scanner sc = new Scanner(System.in); |
Step No. 3 – Read out the word or phrase from the console
1 |
String phrase1 = sc.nextLine(); |
- With the help of String phrase1 we assigned the variable c of the String type the name "phrase1." Why did we choose the String type for the variable? Because, according to the condition of the task, the user needs to enter any word or phrase. Since we expect the user to enter a word or phrase, we chose the String type for the variable phrase1.
- = sc. nextLine (); is responsible for reading out the word or phrase which the user entered in the console. With the help of the nextLine () method, we read out the entered word or phrase from the console and assign it to the variable phrase1.
From now on, the variable "phrase1" will have the phrase or word entered by the user.
Note: In examples 1 and 2, we used the nextInt (); method for whole numbers. In this example, we expected the user to enter a phrase or word (instead of whole numbers), so we used the nextLine (); method.
Then we displayed the word or phrase which the user entered in the console:
1 |
System.out.println(phrase1); |
Example No. 4 – The nextLine () method
Let's suppose that we want the user to enter several words or phrases at the same time and we want the program to display them in the one line.
Solution:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any two words or phrases: "); String phrase1 = sc.nextLine(); String phrase2 = sc.nextLine(); System.out.println(phrase1 + " " + phrase2); } } |
If you run this code on your computer, you will see the following in the console:
Enter any two words or phrases:
And if the user enters the phrase “I love” and then the word “Java”, you will see the following in the console:
I love
Java
I love Java
Commentary:
Again, it takes 3 steps to be able to use the scanner.
Step No. 1 – Import the scanner from the java.util package
1 |
import java.util.Scanner; |
Then we asked the user to enter any two words or phrases:
1 |
System.out.println("Enter any two words or phrases: "); |
Step No. 2 – Declare the scanner
1 |
Scanner sc = new Scanner(System.in); |
Step No. 3 – Read out the word or phrase from the console
1 2 |
String phrase1 = sc.nextLine(); String phrase2 = sc.nextLine(); |
-
- With the help of String phrase1 and String phrase2, we declared the variables phrase1 and phrase2 of the String type. Why did we choose the String type for the variable? Because, according to the condition of the task, the user needs to enter any two words or phrases. So we expect that the user will enter two words or phrases. And that’s why we chose the String type for the variables phrase1 and phrase2.
- = sc. nextLine (); is responsible for reading out the words or phrases from the console. With the help of the nextLine () method, we read out the entered word or phrase from the console and assign it to the variables phrase1 and phrase2.
Then we displayed the 2 words or phrases entered by the user:
1 |
System.out.println(phrase1 + " " + phrase2); |
Note: We added “ “ between phrase1 and phrase2. If we hadn't done this, we would see the following in the console:
I love
Java
I loveJava
Example No. 5 – The next () method
Let's suppose that we want the user to enter any word or phrase and we want the program to display everything in the console up to the first space.
For example, if the user enters “Working with the scanner is cool”, then the program will only read out the word “Working”, because the first space appears after this word.
Or if, for example, the user enters “I love Java”, then the program will only read out the word “I”, because the first space appears after this word.
Anyway, you got the idea. Let’s take a look at the code.
Solution:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any word of phrase: "); String phrase1 = sc.next(); System.out.println(phrase1); } } |
If you run this code on your computer, you will see the following in the console:
Enter any word or phrase:
And if the user enters “I’m the Lord of the Scanner”, you will see the following in the console:
Enter any word or phrase:
I’m the Lord of the Scanner
I’m
Commentary:
Again, it takes 3 steps to be able to use the scanner.
Step No. 1 – Import the scanner from the java.util package:
1 |
import java.util.Scanner; |
Step No. 2 – Declare the scanner
1 |
Scanner sc = new Scanner(System.in); |
Then we asked the user to enter any word or phrase:
1 |
System.out.println("Enter any word of phrase: "); |
Step No. 3 – Read out the word or phrase from the console
1 |
String phrase1 = sc.next(); |
-
- With the help of String phrase1, we declared the variable c of the String type and named it phrase1. Why did we choose the String type for the variable? Because, according to the condition of the problem, the user needs to enter a word or a phrase. Since we expect the user to enter a word or a phrase, we chose the String type for variable phrase1.
- = sc. next (); is responsible for reading out the word or phrase up to the first space in the console. With the help of the next () method, we read out the entered word or phrase which appeared up to the first space from the console and assign it to variable phrase1.
From now on, variable phrase1 will have everything up to the first space.
Then we displayed everything up to the first space in the console:
1 |
System.out.println(phrase1); |
Example No. 6 – The nextDouble () method
Let's suppose you want the user to enter any fraction and for the program to read out the number the user entered and then display it in the console.
Solution:
1 2 3 4 5 6 7 8 9 |
import java.util.Scanner; class Test { public static void main(String args[]){ System.out.print("Enter any fraction: "); Scanner scan = new Scanner(System.in); double number = scan.nextDouble(); System.out.println ("You entered " + number); } } |
If you run this code on your computer, you will see the following in the console:
Enter any fraction:
And if you enter the number 2,0 you will see the following in the console:
Enter any fraction:
2,0
You entered 2.0
Commentary:
WARNING: BUG!!!
Note that in the code, all numbers of the "double" type are always written in the format “2.0”, i.e. separated by a point instead of comma.
And we intentionally entered the number of the double type in the format “2,0” – with the comma. Then the program displayed the number in the correct format: “2.0” – “You entered the number 2.0”
This is a bug. You just need to remember that when you enter the number of the double type in the console, you need to write it separated by the comma. If you enter the number in the console (not in the code) in the format “2.0”, the program will give you an error code!
Again, it takes 3 steps to use the scanner.
Step No. 1 – Import the scanner from the java.util package
1 |
import java.util.Scanner; |
Then we asked the user to enter any fraction:
1 |
System.out.print("Enter any fraction: "); |
Step No. 2 – Declare the scanner
1 |
Scanner scan = new Scanner(System.in); |
Step No. 3 - Read out the number from the console
1 |
double number = scan.nextDouble(); |
-
- With the help of double number, we declared the variable number of the double type. Why did we choose the double type for the variable? Because according to the condition of the problem, the user needs to enter any fraction. So we expect the user to enter any fraction. That’s why we chose the double type for the variable number.
- = sc. nextDouble (); the method of the nextDouble() scanner is responsible for reading out the fraction which the user entered in the console. i.e. with the help of the scan.nextDouble () method, we read out the entered fraction from the console and assign it to the variable "number."
That’s it. From now on, the variable "number" will have the number entered by the user.
Then we needed to display the number entered by the user in the console, and we did it with the help of:
1 |
System.out.println ("You entered " + number); |
LET’S SUMMARIZE
In order to use the scanner, you need to take 3 steps:
Step No. 1 - Import the scanner from the java.util package
1 |
import java.util.Scanner; |
Step No. 2 – Declare the scanner. For example:
1 |
Scanner scan = new Scanner(System.in); |
Step No. 3 – Set the appropriate method for reading out from the console. For example:
1 |
int number = scan.nextInt(); |
Scanner methods that you already know:
next (); - reads the entered line up to the first space
nextLine (); - reads the whole entered line
nextInt (); - reads the entered number of the int type
nextDouble (); - reads the entered number of the double type
Keep in mind that if you use the nextDouble () methods for fractions, you need to write the numbers separated by a comma, instead of period. For example: 7,5
You can find more articles in our Java Tutorial for Beginners.