I am not an expert about the scanner, but here is what I think:
1. Run ScanIn and pass in "\d\d" as arg
2. When the System.out.flush is called, users input a string 3. Scanner s = new Scanner(System.in) means this scanner s takes the input stream from the user and takes the string from step 2.
4. s.findInLine(arg) means the scanner finds the pattern of "\d\d" from the input stream
5. Here is how the findInLine works:
5.1 take the input stream , analyze each token to find a match for "\d\d" regular expression, which means a digit follows by one digit eg 33, 45.
5.2 How to analyze each token in the input stream? Given a string 1b2c335f456, first the scanner takes a look at 1. Yes, 1 is a digit. The scanner looks ahead and see b. No, b is not a digit. So, it looks at b again. No, b is not a digit. Look at 2, yes, it is . Then, it looks ahead and see c. No, it is not. It looks at c again, no it is not. Looks at 3, yes, it is. Then looks ahead, it is a 3 , yes. Bingo! It skips to 5, yes it is, looks ahead ....
5.3 When the scanner looks at the last digit, 6, yes, it is a digit, and then looks ahead , but the scanner does not see any characters at the end. So, the scanner does not find a match when it encounter 6.
5.4 Finally, the scanner stops by the end of the string after 6 and find a null token and output null. (This is not intuitive. But this may be how the scanner is designed.)
I think the findInLine() method is the one to iterate the string.
Awesome explanation. Working of findInLine() explained by you seems good to me.
Helen Ma wrote: When the System.out.flush is called, users input a string
I don't think that this is correct because System.out.flush() is used here to clean the buffer not for taking input. I think findInLine() does two works here
1. iterate the string as explained bu you
2. taking input from user
I checked it by putting System.out.println("A") after every line of code. When this code run, the commend line interface looks like-
1b2c335f456 //input by user(control waits for input at this line, it means findInLine() is taking input at this line)
But am not sure wether it is done by findInLine() method itself? I am confused that how it is done by a single method?
Joined: Nov 01, 2011
Thanks for pointing out. System.in is the input stream that actually takes input from the user. So, when the scanner is instantiated, it is associated with the input stream.
findInLine method takes the regular expression "\d\d" from the argument.
There are basically two different inputs from the users here:
1. "\d\d" is the argument when you execute ScanIn.
2. When you are prompted for input, you input the string into the input stream.
I think the findInLine iterates the input stream and tries to find a match using the regular expression.
I think once System.in takes input from the command prompt, the input is turned into an input stream. The input stream is passed into the scanner's constructor, so that the scanner is associated with the input.
The findInLine() method takes the input, iterate it and find matches.
Helen Ma wrote:System.in is asking for user input.
Are you sure? As per my knowledge System.in is used to connect the stream to input component(keyboard in this case).
And even if you are right, then why it is printing tow As after the statement Scanner s=new Scanner(System.in) and then waiting for input?
Joined: Nov 01, 2011
When I ran this program from the command prompt.
c:\jdk1.6\bin\ > java ScanIn "\d\d"
The scanner s takes the input from the user once. Then, in the do-while loop, the scanner finds matches from the input.