wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using Scanner class in search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using Scanner class in search" Watch "using Scanner class in search" New topic
Author

using Scanner class in search

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

K&B page number 501

I am not able to understand the behavior of method findInLine() in this program. Please someone tell me how it is working? How does methos findInLine() tekes input dynamically?


Astha - OCPJP 6 (90%)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I am not an expert about the scanner, but here is what I think:
1. Run ScanIn and pass in "\d\d" as arg[0]
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[0]) 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.

Correct me if I am wrong.

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Thanks Helen.
Awesome explanation. Working of findInLine() explained by you seems good to me.
but....
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-
Input: A
A
A
A
A
1b2c335f456 //input by user(control waits for input at this line, it means findInLine() is taking input at this line)
found 33
A
found 45
A
found null

But am not sure wether it is done by findInLine() method itself? I am confused that how it is done by a single method?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Helen Ma wrote: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.

Its true, but it needs a methos to take input. Here in this code, we are not invoking next() or nextLine() methods which are generally used for this purpose. Then what is asking user for input?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
System.in is asking for user input.
Visit this web site to see an example about getting input from the command prompt:
http://www.java2s.com/Code/JavaAPI/java.lang/Systemin.htm

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.

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

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?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
When I ran this program from the command prompt.
For example:
c:\jdk1.6\bin\ > java ScanIn "\d\d"
Input: abc123
......

The scanner s takes the input from the user once. Then, in the do-while loop, the scanner finds matches from the input.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Have you included statements for printing 'A' in your code? I included them to test the flow of program. Try it including them. You'll get my problem.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Yes. I run your Test. java. The output is
input: A
A
A
A
A
found null.

I don't know why with the print statement, the system. in is blocked? I think ..... there may be a thread issue with the JVM that the output thread blocks the input thread?

Anyone knows why?
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

No one else taking intrest in this post
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Scanner class in search