aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scanner question or maybe it may be more about java class API's 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 "Scanner question or maybe it may be more about java class API Watch "Scanner question or maybe it may be more about java class API New topic
Author

Scanner question or maybe it may be more about java class API's

Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
Hi ranchers

I've been reading the Scanner class API web page found on Sun's web site(http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html).

It includes the following code snippet:

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i);
s.close();

What has confused me is that is the method summary on the same page, it states that findInLine returns a String value. The above example clearly does not return a value into a String. My question is as follows:

Is it the case that because it returns a value that is sub-classed off Object that it imlicitly performs a "new String()" and creates an anonamous instance of the object (in this case a String)? Or is there something else happening here?


If at first you don't succeed, try, try again. If you don't try you have failed.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What has confused me is that is the method summary on the same page, it states that findInLine returns a String value. The above example clearly does not return a value into a String.


The findInLine() method you are referring to does return a String, the return value is just ignored in this case. You could choose not to ignore it however:


Although in this case the entire input String would be returned.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
jan ter avest
Ranch Hand

Joined: Dec 18, 2006
Posts: 46
I asked this question yesterday!

http://www.coderanch.com/t/261497/java-programmer-SCJP/certification/Scanner-MatchResult

Anyway, the result of method s.findInLine() may be ignored but somehow its pattern is used in s.match().

How do you explain that?
[ February 12, 2007: Message edited by: jan ter avest ]

SCJP1.5 SCWCD1.5
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Javadocs for Scanner.java.

match

public MatchResult match()

Returns the match result of the last scanning operation performed by this scanner. This method throws IllegalStateException if no match has been performed, or if the last match was not successful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scanner question or maybe it may be more about java class API's