• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked this question yesterday!

https://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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic