Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tokenizing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tokenizing" Watch "Tokenizing" New topic


Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

searching using the scanner Class
K&B book pg no-501

The invocation and input
java ScanIn "\d\d"
input: 1b2c335f456
produce the following:
found 33
found 45
found null

why is null in the output ?
i am not able to understand it.

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

Since we have a do-while loop, so after 45 is matched, we look for another match, no match is found so s.findInLine returns null. We display null and then check if nothing was found and then end the loop...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
If you do the program as follows then you will not get null as output:

In this case, your while loop body will not be executed if the token is null. Actually, here the loop condition is test first and then print the output (execute the loop body) when condition is true, but, in your case the output is printed first (execute the loop body) then test the loop condition for the next iteration.

M.Sc. in CS, OCPJP6 93%
I agree. Here's the link:
subject: Tokenizing
It's not a secret anymore!