This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scanner and MatchResult Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Scanner and MatchResult" Watch "Scanner and MatchResult" New topic
Author

Scanner and MatchResult

jan ter avest
Ranch Hand

Joined: Dec 18, 2006
Posts: 46
The following code is from the WhizLabs simulator:
class ScannerTest {
public static void main( String[] args ) {
String input = "1 done 2 done all done";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) done (\\d+) done (\\w+)

done");
MatchResult result = s.match();
for (int i=1; i<= result.groupCount(); i++)// line a
System.out.print(result.group(i)+ " " ); // line b
s.close();
}
}

the code runs and compiles and output 1 2 all.

Here's my question: if I replace line a and b with just:
System.out.print(result.group() + " ");

Why does the code throw a runtime IllegalStateException, no match result available??


SCJP1.5 SCWCD1.5
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Aghhhhhhhhhhhh. I have seen that Interface in Whizlabs too!!!. Does MatchResult come in the exam???


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
jan ter avest
Ranch Hand

Joined: Dec 18, 2006
Posts: 46
Not according to the KB book.

WhizLabs tends to squeeze in some off-topic/extra material in its questions, in my opinion.

Btw, know of any other recommendable simulators? Im only using the whizlabs one at the moment...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner and MatchResult
 
Similar Threads
Reg Expressions
Mock question java.util.regex.Pattern - drag and drop
Scanner question or maybe it may be more about java class API's
Possessive Quntifiers
groupcount() method in MatchResult Interface in Java 5