Scanner in Java (input and output)

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

1

2

We suggest you to recall two real-world examples, which will be very helpful for the learning of the topic.

  1. When we are on a journey, staff members of an airport check your luggage with the scanner machine. Here is your bag at the entrance. The machine scans your bag and an airport worker knows exactly what do you have in it.
  2. Scanners in stores work the same way. Here is a bar code, a seller scans the bare code and now he knows everything about the product that is listed under this bar code.

There are similar problems in the world of Java programming. For example, you often need to solve the following problems:

  • The user enters some number in the console, and the program has to read out the number entered by the user from the console.
  • The user enters some word in the console, and the program has to read out the word entered by the user from the console.

For solving these problems Java uses the scanner. Remember: if something is entered in the console and you need to read out it, you need to use the scanner.

Following are valid examples, which will help you to:

  1. understand in practice how does the scanner work. There are 6 examples of the code in this article. We recommend you to run these codes on your computer and learn in practice how does it work.
  2. master four scanner methods:

next ();

nextLine ();

nextInt ();

nextDouble ();

Methods are, roughly speaking, the actions that can be performed by the Scanner. In fact, there are a lot more scanner methods. But at this stage it’s enough for you to know these four methods. Let’s get started!!!

Example No.1 – with the nextInt () method

Let us assume that we want the user to enter any integer from 1 to 10 in the console, and the program need to display the number entered by the user.

Given that we need to “scan” the number entered by the user, it makes perfect sense – we need to use the scanner. Following are the problem solution and comments to it.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter any integer from 1 to 10:

Then, for example, if you enter the number 5 you will see the following in the console:

Enter any integer from 1 to 10: 5

You entered 5

Comments:

In the article What is Java class library we figured out that Java has the huge library of tested code – ready-made frameworks for many problems faced by programmers in their daily work. We also talked about different packages, classes and methods. So, now we are going to work with the java.util package. This package includes the Scanner class. Also it has methods (actions), which allow us to work with input and output of information to the console.

scanner_vertex-academy_en

In order to be able to use the Scanner class we need to take 3 steps.

Step No.1 – it’s necessary to write the following line in the code:

Why would we write this line? By means of it we imported the Scanner class from the java.util package. If we would forget to write it, the scanner simply wouldn’t be able to work. Also you need to turn your attention to the place where we wrote the line.

Then we asked the user to enter any integer from 1 to 10. To do this, we wrote

Step No.2 – to declare the scanner

Scanner2 Vertex Academy

  1. 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. You can call it whatever you want, you don’t need to call it “scan”.
  2. Then we write = new Scanner (System.in);

When you will come to the topic of classes, you will gain a greater understanding of this construction. At this stage this information will be enough for your understanding of current topic.

Step No.3 – to read the number from the console

    1. 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 problem the user need to enter any integer from 1 to 10. Respectively, we expect that the number entered by the user will be integer. That’s why we chose the int type for the variable number.
    2. = scan. nextInt ();  the method of nextInt () scanner is responsible for reading out the number entered by the user from the console. I.e. with the help of scan.nextInt () we read out the entered integer 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 entered by the user in the console and we did it with the help of this line:


Example No.2 – with the nextInt () method

Ask the user to enter any two integers. After that you need to display the sum on these two numbers in the console.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter any two integers:

Then, for example, if you enter numbers 2 and 3, you will see the following in the console:

Enter any two integers:

2

3

5

Comments:

Again, let’s take 3 steps to be able to use the scanner.

Step No.1 – to import the scanner from the java.util package

Then asked the user to enter two any integers

Step No.2 – to declare the scanner

  1. With the help of Scanner k we declared that the variable will be called “k” and it will fall into the Scanner class. It’s necessary to write the word Scanner in front of the name of the variable. You can call it whatever you want, you don’t need to call it “k”.
  2. Then we write = new Scanner (System.in);

Step No.3 – to read out the number from the console

What is more, we did it two times, because we asked the user to enter two different numbers.

That’s it, from now on the variables number1 and number2 will have two numbers entered by the user.

And then we displayed the sum of number1 and number2


Example No.3 – with the nextLine () method

Let us assume that we want the user to enter any word and phrase in the console, and we would be able to display the word or phrase he entered.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter any word or phrase:

Then, for example, if the user will enter “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

Comments:

Again, we took 3 steps to be able to use the scanner.

Step No.1 – to import the scanner from the java.util package

Then we asked the user to enter any word of phrase

Step No.2 – to declare the scanner

Step No.3 – to read out the word or phrase from the console

  1. With the help of String phrase1 we declared the variable c of the String type, which was called phrase1. Why did we choose the String type for the variable? Because according to the condition of the problem the user need to enter any word or phrase. Respectively, we expect that the user will enter a word or a phrase. That’s why we chose the String type for the variable phrase1.
  2. = sc. nextLine (); is responsible for reading out the word or phrase from the console. I.e. 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.

Now 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 the integers. In this example we expect the user to enter the (phrase or word) instead of integers, for this reason we use the nextLine (); method.

Then we display the word or phrase entered by the user in the console:


Example No.4 – with the nextLine () method

Let us assume that we want the user to enter several words or phrases at the same time, and we would be able to display them in the one line.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter two any words or phrases:

Then, for example, if the user enter the phrase “I love” and then the word “Java”, we will see the following in the console:

I love

Java

I love Java

Comments:

Again, we took 3 steps to be able to use the scanner.

Step No.1 – to import the scanner from the java.util package

Then we asked the user to enter any two words or phrases

Step No.2 – to declare the scanner

Step No.3 – to read out the word or phrase from the console

    1. 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 problem the user need to enter any two words or phrases. Respectively, we expect that the user will enter two words or phrases. That’s why we chose the String type for the variables phrase1 and prase2.
    2. = sc. nextLine (); is responsible for reading out the word or phrase from the console. I.e. 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 2 words or phrases entered by the user

Note: We added “ “ between phrase1 and phrase2. If we didn’t do this, we would see the following in the console:

I love

Java

I loveJava


Example No.5 – with the next () method

Let us assume that you want the user to enter any word or phrase. And at the same time you want the program to display everything up to the first space to the console.

For example, if the user enters “Working with the scanner is cool”, then the program will read out only the word “Working”, because the first space stays after this word.

Or if, for example, the user enters “I love Java”, then the program would read out only the word “I”, because the first space stays after this word.

Anyway, you got the idea. Let’s take a look at the code.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter any word of phrase:

Then, for example, if the user enter “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

Comments:

Again, we took 3 steps to be able to use the scanner.

Step No.1 – to import the scanner
from the java.util package

Step No.2 – to declare the scanner

Then we asked to user to enter any word or phrase

Step No.3 – to read out the word or phrase from the console

    1. With the help of String phrase1 we declared the variable c of the String type, which is called phrase1. Why did we choose the String type for the variable? Because according to the condition of the problem the user need to enter a word or a phrase. Respectively, we expect that the user will enter a word or a phrase. That’s why we chose the String type for the variable phrase1.
    2. = sc. next (); is responsible for reading out the word or phrase up to the first space from the console. I.e. with the help of the next () method we read out the entered word or phrase up to the first space from the console and assign it to the variable phrase1.

Now the variable phrase1 will have everything up to the first space.

Then we display everything up to the first space in to console.


Example No.6 – with the nextDouble () method

Let us assume you want the user to enter any fraction. And the program need to read out the number entered by the user and display it in the console.

Solution:

If you try to run this code on your computer, you will see the following in your console:

Enter any fraction:

Then, for example, if you enter the number 2,0 you will see the following in the console:

Enter any fraction:

2,0

You entered 2.0

Comments:

Bug Vertex Academy

WARNING, THE 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 help of comma. That said, the program displayed the number in the correct format “2.0” – “You entered the number 2.0”

This is the bug. You just need to remember, that if you enter the number of the double type in the console you need to write it separated by the comma. If you would enter the number to the console in the format “2.0”, the program would return you an error code!

Again, we took 3 steps to be able to use the scanner.

Step No.1 – to import the scanner from the java.util package

Then we asked the user to enter any fraction

Step No.2 – to declare the scanner

Step No.3 - to read out the number from the console

    1. With the help of double number we declared the variables 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 need to enter any fraction. Respectively, we expect that the user will enter any fraction. That’s why we chose the double type for the variable number.
    2. = sc. nextDouble (); the method of the nextDouble() scanner is responsible for reading out the fraction entered by the user from 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 variables number.

That’s it, now the variable number will have the number entered by the user.

Then we need to display the number entered by the user to the console, and we did it with the help of


LET’S SUMMARIZE

In order to use the scanner, you need to take 3 steps:

Step No.1 - to import the scanner from the java.util package

Step No.2 – to declare the scanner. For example:

Step No.3 – to call the appropriate method for reading out from the console. For example:

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

You also should remember that if you use the nextDouble () methods for fractions, you need to write the numbers separated by comma instead of point. For example: 7,5

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--