Using Scanner in Java + Read String With Spaces

1

2

This is one of the articles from our Java Tutorial for Beginners.


Nosotros suggest that you call back two real-world examples which will help you understand the topic of this commodity.

  1. When we're about to go on a flight, the staff at the airport start bank check your luggage with a scanner machine. The machine scans your bag and the airport employee finds out exactly what you have in it.
  2. Scanners in stores work the same style. An employee at a check-out stand up scans a bar code and sees all the data that is listed under that bar code.

At that place are similar tasks in the earth of Java programming. For example, it's oftentimes necessary to deport out the following tasks:

  • A user enters some number in the console and so the plan has to read out the number which the user entered in the console.
  • A user enters some word in the console and so the programme has to read out the word which the user entered  in the panel.

To carry out these tasks, Java uses the scanner. Call back: if something is entered in the panel and you desire to read what was entered, then you need to apply the scanner.

We will examine several examples of lawmaking, after which you lot volition:

  1. sympathize how the scanner works in exercise. There are vi examples of code in this article. We recommend that you run all of them on your computer and larn how they work in practice.
  2. master four scanner methods:

side by side ();

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, information technology will be sufficient to know but 4. Allow's get started!

Example No. ane – The nextInt () method

Let's suppose that we want the user to enter any integer from i to x in the console, and nosotros want the programme to display the number that the user entered.

Since we need to "scan" the number entered the user entered, nosotros need to use the scanner. You can come across the solution below, as well as comments on information technology.

Solution :

If yous run this lawmaking on your calculator, y'all will meet the post-obit in the panel:

Enter any integer from i to 10:

And if y'all enter, for example, the number 5, y'all volition see the following in the console:

Enter whatsoever integer from one to 10: 5

You entered 5

Commentary :

In the article " What is the Java Class library? " nosotros learned that Java has a huge library of tested code – prepare-fabricated frameworks for many problems that programmers face in their daily work. We also talked near diverse packages, classes, and methods. So, at present we are going to work with the java.util parcel. This packet includes the Scanner class, which has methods (actions) which allow united states of america to work with the input and output of data in the console.

scanner_vertex-academy_en

In club to be able to apply the Scanner class, we first need to have iii steps.

Step No. 1 – Write the following line in the code:

Why did nosotros write this line? It helped us import the Scanner class from the coffee.util parcel. If we had forgotten to write it, the scanner but wouldn't work. Too, take note of where nosotros wrote the line.

Then we asked the user to enter whatever whole number (integer) from 1 to 10. To practice this, we wrote:

Step No. 2 – Declare the scanner

Scanner2 Vertex Academy

  1. With the assist of Scanner scan , we alleged that the variable will be called "scan" and it will autumn into the Scanner class. It'due south necessary to write the give-and-take Scanner in front of the name of the variable. By the fashion, you can phone call it whatsoever you lot want; yous don't need to call it "scan."
  2. And so we wrote = new Scanner (System.in);

When you get to the topic of classes, y'all volition gain a greater understanding of this construction.

Step No. three – Read the number from the console

    1. With the help of int number , we alleged the variable number of the int type. Why did we cull the int type for the variable? Considering, according to the status of the task, the user has to enter any integer from 1 to x. And since we expect that the number entered past the user will exist a whole number (an integer), we chose the int type for the variable "number."
    2. = 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 past the user . Hurray!

And so nosotros needed to enter the number that the user entered in the panel and we did information technology with the help of this line:


Instance No. 2 – The nextInt () method

Ask the user to enter any two whole numbers (integers) . Subsequently that, you demand to display the sum of these 2 numbers in the panel.

Solution:

If run this code on your computer, yous will meet the following in the panel:

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

v

Commentary:

Again, it takes iii steps to use the scanner.

Footstep No. i – Import the scanner from the java.util package:

Then nosotros asked the user to enter two whatsoever integers:

Step No. two – Declare the scanner

  1. With the help of Scanner k , we alleged that the variable will exist chosen "one thousand" and will autumn into the "Scanner" class. It's necessary to write the word "Scanner" in front of the proper noun of the variable. By the fashion, yous can call it whatever you desire; you don't need to call it "thousand."
  2. Then we wrote = new Scanner (System.in);

Step No. 3 – Read out the number from the console

Really, we did this two times, because we asked the user to enter ii different numbers:

That's it. From now on, the variables "number1" and "number2" will take the two numbers that the user entered.

And then nosotros displayed the sum of "number1" and "number2":


Example No. 3 – The nextLine () method

Let's suppose that nosotros want the user to enter whatever word or phrase in the panel and we want the program to display the word or phrase the user entered.

Solution:

If y'all run this lawmaking on your computer, yous will encounter the following in the console:

Enter any word or phrase:

And if the user enters "I'm the Lord of the Scanner", y'all will meet the following in the panel:

Enter any word or phrase:

I'k the Lord of the Scanner

I'g the Lord of the Scanner

Commentary:

Again, it takes 3 steps to use the scanner.

Step No. 1 – Import the scanner from the coffee.util bundle:

And then we asked the user to enter any word or phrase:

Step No. 2 – Declare the scanner

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

  1. With the help of String phrase1 we assigned the variable c of the String type the name "phrase1." Why did nosotros cull the Cord type for the variable? Because, according to the condition of the task, the user needs to enter any word or phrase. Since we await the user to enter a discussion or phrase, we chose the String type for the variable phrase1.
  2. = sc. nextLine (); is responsible for reading out the give-and-take or phrase which the user entered in the panel. With the help of the nextLine () method, we read out the entered discussion or phrase from the panel and assign it to the variable phrase1.

From now on, the variable "phrase1" will have the phrase or word entered by the user.

Annotation: In examples one and 2, we used the nextInt (); method for whole numbers. In this example, we expected the user to enter a phrase or discussion (instead of whole numbers), so we used the nextLine (); method.

And so we displayed the word or phrase which the user entered in the console:


Instance No. four – 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 programme to display them in the one line.

Solution:

If you run this lawmaking 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 "Coffee", you will see the post-obit in the console:

I love

Java

I love Java

Commentary:

Once more, it takes iii steps to be able to use the scanner.

Step No. 1 – Import the scanner from the coffee.util package

Then we asked the user to enter any ii words or phrases:

Step No. ii – Declare the scanner

Step No. iii – Read out the discussion or phrase from the console

    1. With the help of String phrase1 and String phrase2 , we alleged the variables phrase1 and phrase2 of the String blazon . Why did we cull the String type for the variable? Because, according to the condition of the task, the user needs to enter whatsoever 2 words or phrases. So we look that the user volition enter 2 words or phrases. And that's why we chose the String type for the variables phrase1 and phrase2.
    2. = 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 discussion or phrase from the panel and assign it to the variables phrase1 and phrase2.

Then we displayed the 2 words or phrases entered by the user:

Note: We added " " betwixt phrase1 and phrase2. If we hadn't washed this, nosotros would see the following in the panel:

I love

Java

I loveJava


Instance No. five – The side by side () method

Allow's suppose that nosotros want the user to enter whatsoever give-and-take or phrase and we desire the program to display everything in the console upward to the commencement space.

For case, if the user enters "Working with the scanner is cool", then the program will but read out the word "Working", considering the commencement space appears afterward this discussion.

Or if, for example, the user enters "I beloved Java", then the program volition just read out the word "I", considering the starting time space appears after this word.

Anyway, you got the thought. Let's take a look at the code.

Solution:

If yous run this code on your estimator, you volition see the following in the panel:

Enter any word or phrase:

And if the user enters "I'grand the Lord of the Scanner", you volition run across the following in the console:

Enter any word or phrase:

I'm the Lord of the Scanner

I'm

Commentary:

Again, information technology takes 3 steps to be able to use the scanner.

Step No. 1 – Import the scanner
from the java.util packet:

Stride No. ii – Declare the scanner

Then nosotros asked the user to enter whatever word or phrase:

Stride No. 3 – Read out the word or phrase from the panel

    1. With the help of Cord phrase1 , we declared the variable c of the String type and named information technology phrase1. Why did we choose the String type for the variable? Because, co-ordinate to the condition of the problem, the user needs to enter a discussion or a phrase. Since we await the user to enter a word or a phrase, we chose the Cord blazon for variable phrase1.
    2. = sc. side by side (); is responsible for reading out the word or phrase up to the first space  in the console. With the assistance of the side by side () method, we read out the entered discussion or phrase which appeared upwardly to the first infinite from the panel and assign information technology to variable phrase1.

From now on,  variable phrase1 will have everything upwards to the first space.

So we displayed everything upwardly to the first infinite in the console:


Example No. half-dozen – The nextDouble () method

Permit'south suppose you want the user to enter any fraction and for the plan to read out the number the user entered then display it in the panel.

Solution:

If you run this code on your figurer, you will see the following in the console:

Enter any fraction:

And if you enter the number ii,0 you lot volition see the following in the panel:

Enter any fraction:

two,0

You entered 2.0

Commentary:

Bug Vertex Academy

Warning: Bug!!!

Note that in the code, all numbers of the "double" type are always written in the format "2.0", i.east. separated by a point instead of comma.

And we intentionally entered the number of the double type in the format "ii,0" – with the comma . Then the program displayed the number in the correct format: "two.0" – "You entered the number 2.0"

This is a bug. Y'all merely need to remember that when you enter the number of the double blazon in the panel, you lot need to write it separated by the comma. If y'all enter the number in the console (not in the code) in the format "two.0", the program will give y'all an fault code!

Again, it takes iii steps to employ the scanner.

Step No. 1 – Import the scanner from the java.util package

And so we asked the user to enter any fraction:

Step No. 2 – Declare the scanner

Pace No. 3 - Read out the number from the console

    1. With the help of double number , we declared the variable number of the double blazon . 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 nosotros expect the user to enter whatsoever 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 which the user entered in the console. i.eastward. with the assistance of the scan.nextDouble () method, we read out the entered fraction from the console and assign it to the variable "number."

That's information technology. From at present 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 assistance of:


Allow'S SUMMARIZE

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

Footstep No. 1 - Import the scanner from the java.util bundle

Step No. 2 – Declare the scanner. For instance :

Step No. 3 – Prepare the advisable method for reading out from the console. For case:

Scanner methods that y'all 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 yous use the nextDouble () methods for fractions, yous need to write the numbers separated by a comma, instead of period. For example: 7 , 5

You can discover more articles in our Java Tutorial for Beginners.


barronseetumbrave1962.blogspot.com

Source: https://vertex-academy.com/tutorials/en/work-scanner-java-input-output/

0 Response to "Using Scanner in Java + Read String With Spaces"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel