aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pattern Matching Query from Exam LAb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pattern Matching Query from Exam LAb" Watch "Pattern Matching Query from Exam LAb" New topic
Author

Pattern Matching Query from Exam LAb

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634






The first segment gives compiler error .

while,the second one compiles and runs fines..
In both of the codes,mt.group() is outside of while
Any reason for this ???


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

The first segment gives compiler error

No it doesn't. It compiles fine but generates an exception at runtime as there is no match for group method to return. In the second case you don't get an exception as there is a match which is returned by the group method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Ankit garg wrote:
generates an exception at runtime as there is no match for group method to return. In the second case you don't get an exception as there is a match which is returned by the group method...


why does mt.group() in the first code fails to give any result (outside of while) ?
I read from java API that it returns the last Found match
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

You already found the last match inside of the loop in the first piece of code. Then when the loop ends there is nothing to match, that's why you get an exception. Your second code will also fail if you add another call to group method like this
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

thanks Ankit

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pattern Matching Query from Exam LAb