• 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

MatchResult.start() & MatchResult.end() methods

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

Here are 2 programs given in JavaBeat Mock exam which are behaving differently.


The above program's output is 5
The next program goes like this...


And the output of this is 2 followed by 8

In the first program we are using scanner.match.start(). In the second one we are using scanner.match.end(). What's the indexing of these two methods? Do they use zero as starting index or 1? Can any one explain me their behavior??? Thanks all!
[ July 08, 2008: Message edited by: Swapna Gouri Kalanidhi ]
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swapna Gouri,

I'll try to explain the results. Let's start with the first one.

1. The first scanner.next() finds "ai ".
2. The second scanner.next() finds "ello".
3. scanner.match().start() returns the starting position of the substring "ello" in the original string "hai hello". This
is 5.
4. scanner.match().end() returns the position after the substring (see API documentation for MatchResult). This is 9.

The second example works in the same fashion. If it still makes problems, just let us know.
[ July 08, 2008: Message edited by: Ralph Jaus ]
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ralph, I got the point. But I still have a doubt, does these methods,viz., match.start()& match.end() use zero indexing or 1 indexing? I mean should we start counting the alphabets from 0 or from 1??
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ralph
Thanks for explaining the behavior of the start() & end() methods. i have tried both the methods practically & I have understood the concept. Thanks a lot for giving a detailed description how both of them work...!
 
reply
    Bookmark Topic Watch Topic
  • New Topic