Scanner uses the delimiter pattern to read your data.
Before we continue with our dealership, you should understand how delimiters work.
The first thing I'll need you to do is create a new class by yourself inside Section three folder,
create a new file name, delimiters Java and make sure the delimiters class has a main method.
The delimiter separates your user input and the delimiter for user input is whitespace by default.
So if you input three numbers separated by a white space, it takes three scanned next in methods to
pick them up.
Each method skips the delimiter, it skips the white space, and it picks up the next integer.
It doesn't matter how much white space there is between each input scandal and skips the delimiter and
it reads the next integer that you input.
Even if you press enter many times between each input next and skips all the white space and it reads
the next input and because next and skips the delimiter, we say that it follows the delimiter pattern.
Next internship's the delimiter and reads the next integer value, next long skips the delimiter and
reads the next long value.
Next double also skips to the limiter and reads the next double value.
Next skips the delimiter and reads the next string value.
Hope you're ready, because we're about to write a lot of code.
First time and ask the user to.
NASA managers and for the sake of example, am going to ask them to write each value on the same line.
Then I'm going to copy this.
And ask them to enter two very big integers.
To decimals.
And to tax the values.
Now we're going to pick up every value using the appropriate scanner method, so first I'm going to
set up a new scanner right scanner, wait for the auto import.
Skin is equal to a new skin, for instance, that can receive input from the system system in.
First, we're going to need to scan next and methods, and no one is equal to the next integer.
And number two is equal to the integer that follows.
Then we're going to need to scan the next long values.
Big no one is equal to the next long value.
Big number two is equal to the scandal next long value.
Now for decimals, we use next double double decimal one is equal to Scandi, next double.
And double decimal two is equal to scandal next double.
And finally, to pick up text we're not going to use next line, we're going to use Scandal next because
unlike next line, each scan, the next method is going to follow the delimiter pattern.
Such that it's going to set the delimiter, the white space that you put in between each value, it's
going to skip that and pick up only the next value that it sees.
All right, as always, we're going to close scanner scanned close.
And that was a lot of code, we're almost done.
The last thing we're going to do is print the values that scanner picks up.
So we'll print remember, we can use the escape character to create a type of space and we'll say the
managers are Kolan.
No one break the string at some space between printing number one and number two.
Now, I'm going to copy this four times.
And here will say big integers and we'll print both numbers that we pick up.
There will say decimals.
And we'll print both decimals that we pick up.
And here will say words and will print both words.
That was a lot of code, and I'm happy to say that we're done.
Let's run it and see what happens.
Here, I'm going to put two integers.
Two very big injuries.
You decimals.
And two words.
And everything worked out, we successfully picked up every value and printed it.
Each scanner method skips the delimiter, it skips the whitespace, and it picks up only the next value
that it sees.
It doesn't matter how much white space there is between each value, there could be 20 lines worth of
white space.
It skips all of it and it only picks up the next value that it sees.
What about skin next line, scandal, next line reads the entire line, it doesn't care about delimiters,
it reads the entire line, including all of the white space in between each value.
If you scan the next line, it's going to read this whole line is one input.
We're going to try this in our code.
I'm going to replace each method with scanned next line.
I'm going to say string numbers is equal to everything that appears in the next line.
It's going to read every value in that line and return it as a string.
Then we'll say string big numbers is also equal to all the values that appear in the next line.
And we'll do the same thing for decimals.
And string text is also going to be equal to all the values that appear in the next line.
I'm going to rerun the code.
Put in some values.
This is what separates Scandi next line from every other Skinner method.
It reads everything line by line.
So all of this boils down to one question why exactly do we get this scandal next line trap?
Where does it come from?
We talked about this before.
You will fall into this trap if you put scandal next line ahead of next into next double next long or
scandal next.
So we're going to delete the code we have so far or you can comment it out if you want.
It's up to you.
I want to start by showing you the trap and explaining why it happens.
So I'm going to ask the user to enter a no print, please enter a no, then I'm going to pick up the
number with scanned nexted.
And we ask the user to enter some text.
And I'll pick up the text with scanned next line.
Don't forget, stand up close, I shouldn't have removed it.
In any case, I'm going to run this code.
And it seems to skip scan next line, or is it what's really happening is that scan next and picks up
the number, but the line isn't over and scanned.
The next line starts here and it says, OK, I guess this is the next line.
So it reads the rest of the current line, which ends up being completely empty.
So you're scanned.
The next line is actually wasted on an empty line.
And we can fix this issue by calling a throwaway scan next line just before our real scanned next line.
So if we rerun this code.
Everything works out.
What's happening is that next in picks up the integer and the throwaway next line is wasted on the rest
of the current line, which is empty.
And then the following Scandi next line is ready to read all of the values on the next line.
In this case, it reads every sentence.
I love Java.
These methods use the delimiter pattern to read your data scanned next and next long necks double and
next, that is, they skip empty space and they read the next input.
Now scan the next line doesn't care about delimiters.
It reads everything line by line.
And that's why it's liable to falling into a trap.
And the trap occurs when you put Eskandar next line ahead of any method that follows the delimiter pattern
next and next double next log or next.
All of these methods only pick up the values that they're interested in and they leave us with an empty
line that scan the next line is wasted on.
And so the solution is to add a throwaway next line just before your real next line.