Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tokenizing

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic