Vienneau understand how to read text from a file as well as from the network. 2
Next thing to learn is how the can read text. 3
Did they use it. 4
And does it take cancel reading from console is not that common compared to reading. 5
Phylogenic book but still there may be instances where we would need that. 6
For example let's say first typing a server using a program. 7
Did the java program may take some input from the system admin or the developer. 8
And based on the input it may do some preliminary checks on only if all of the checks would succeed 9
then the observer would be started by the program. 10
So that's just one example. 11
And there can be many instances where this might be very useful reading from the Ansal self learned 12
about how we can read text that user enters in a console. 13
So here we have this new method called Read from standard book as we want to read data from the console 14
standard input is basically the console and it is in the eye or Demo class that is in the package. 15
OK. 16
So here initially we are asking the user to enter first start the next stock in order to continue. 17
And we are saying we are using buffered reader and that's because later we will also use a different 18
approach to take the input from the console. 19
But first let's look at this particular approach. 20
And we already discussed it in one of the previous lessons. 21
So we are just going to implement the same code here. 22
In fact the code is already here and what we're going to do is the user is going to enter the next stock 23
. 24
But if the user does not enter the tech stock on you if he enters something else then we would prompt 25
the user to re-enter the text and we were budak under the user enters the next dock. 26
So we use the why loop here for that Ironmen when the user and does the text dock then we would exit 27
the while loop. 28
OK. 29
So here we have the play with resources in packs and we have the normal read button which is a buffer 30
reader which is used for performance. 31
It is chained to the input stream reader that is don't change to an input stream instance. 32
So in this case the input stream instances system. 33
And so on is a variable static variable within the system class and that represents an input stream 34
instance. 35
In this case it represents a console. 36
OK under Carter and coding is UTF 8. 37
So since this is an input stream instance we can pass system not in our we can pass a file input stream 38
or even an input stream that represents a remote host. 39
OK we have seen that in the last lesson and once again we are going to use the read line method from 40
the buffer reader which gives us a line of text as a string and it is assigned to this variable cardiod 41
which is a string. 42
And if it is not equal to none and if it is if it not equal stock then it means that we need to prompt 43
the user to re-enter the text and that's what it is doing. 44
But if the user does start then we would exit the while loop and we would print the next contract. 45
So let's just go ahead. 46
I'm not on this program. 47
OK so it is asking us to enter text. 48
So let's just put some random here. 49
OK. 50
So it's once again asking us to enter the next let's decide which you start and there you go. 51
So we are in the next day. 52
So that's how you will do it. 53
So everything is the same except that we are passing system dot and it's a different source. 54
Now there is also another approach to handle this so you can obviously you can definitely use this matter 55
but there is also another class which helps us to do it on it's call as the scanner class from Jellico 56
Trudel package and it was introduced in giallo file and it can be used for this for doing this. 57
So the input to the scanner constructor is System. 58
In. 59
OK. 60
So this is how you would instantiate a scanner and everything else is say we are asking the user to 61
enter the text dock and instead of red line we have this medicore next line which would read under data 62
the data that is read from the console. 63
So this next line method actually blocks under the user enters a text. 64
And if it equals start then we are going to exit the loop. 65
So let's just go ahead and run this. 66
So once again I put some random because store under Reagan and let's just enter start. 67
There you go. 68
So it's exactly identical to that. 69
And the cool thing about Skynet is that 70
it's not restricted to just strings. 71
For example with buffler reader we were reading on E-Strings But here we can also the token that we 72
enter in the console can also be an end or it can also be along so the text that we understand can be 73
some primitive also need not be just string. 74
So we can also do that. 75
So it kind of provides nice convenient methods to extract the text entered as an OK so we can do that 76
. 77
It can also be done using a buffer reader but in that case we would have to parse the string into and 78
put in or do our set. 79
So we we would have to use something like an integer Dropbox and method. 80
But here it is much more cleaner and the scanner itself provides a method to extract the string as an 81
and so let's just go ahead and vote on it again. 82
So that's understocked underestimate does asking a store and or some stock good. 83
So let's just say one two three. 84
And as you can see it's spending this next thanks to you under the code 1.3. 85
And there are also methods for like next long necks double and so on. 86
You can use that. 87
But if you enter at something else like if you're walking next and but if the string happens to be something 88
else not on and then it will throw an exception card mis input mismatch exception input mismatch exception 89
which is a runtime exception. 90
OK. 91
So let's also try that. 92
Now let me just enter the H.C. as you can see Strawson and put mismatch exception. 93
So that's the thing that's there. 94
What did you have with the scanner. 95
It also uses internally a buffer just like buffer reader. 96
But I think the buffer size is smaller. 97
That's what I read. 98
So that's about scanner and this is a little bit of description from the API itself. 99
So it's not simply a scanner. 100
OK. 101
Parses Remedios and string. 102
OK. 103
And there is nothing more to it or simply a scanner. 104
And the way it works is in this case we passed system not in but we can also pass a string to it or 105
lauching for example or it can be a file also because the input parameter is to the constructor as this 106
is an input stream. 107
So obviously you can pass a file also using the file input stream and it would beg to be taxed on it 108
would tokenize the text which means that it would break the text into individual pieces individual words 109
and it uses it as a delimiter. 110
By default it uses whitespace. 111
So that's how it works. 112
But you can also pass a different delimiter Also if you want to. 113
And and then once you do that you can extract each text using methods like next and our next there is 114
also a method called next order if you want the entire line itself because the text might have several 115
lines then you would use next line. 116
So it is basically used to parse text. 117
OK. 118
And here let's also try that. 119
So we have this text. 120
Hello how are you. 121
So that's the string. 122
So we are invoking a different constructor here and we are just checking if the scanner has another 123
token and if it does then we are printing that. 124
So we're going to print tokenize and burn this strings up this individual words into that. 125
So as you can see it is printing the individual words. 126
Hello how are you. 127
OK. 128
Because it's tokenized using whitespace. 129
And that's how it works by default. 130
OK. 131
So there are also these convenient methods like has Next. 132
Next and even all the Remedios all the methods that can extract the primitives next and has a matter 133
convenient math associated Metuchen has. 134
And it's just going to check if the next book and whether it is an end or not if what it does and if 135
it says Yes if it returns a boolean value or group then you can extract. 136
It doesn't. 137
OK you can invoke this method. 138
So it has those convenience methods. 139
So you can actually go ahead and use this on anytime you want to parse a text. 140
You can think of using a scanner for reading from the console you can use either Gunner or the board 141
reader. 142
And that's about it. 143
You can find the find this good updater. 144
I would more and the resources section. 145
And thank you and happy coding.