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

Scanner and MatchResult

 
jan ter avest
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aghhhhhhhhhhhh. I have seen that Interface in Whizlabs too!!!. Does MatchResult come in the exam???
 
jan ter avest
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic