• 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

K&B Study Guide for Java 5 p498 Selftest problem 1

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Given:



And the command line:
java Regex2 "\d*" ab34ef
What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails.
Answer:
® 3 E is correct. The \d is looking for digits. The * is a quantifier that looks for 0 to many
occurrences of the pattern that precedes it. Because we specified *, the group() method
returns empty Strings until consecutive digits are found, so the only time group() returns
a value is when it returns 34 when the matcher finds digits starting in position 2. The
start() method returns the starting position of the previous match because, again,
we said find 0 to many occurrences.
®˚ A, B, C, D, E, F, and G are incorrect based on the above.
(Objective 3.5)



QUESTION: The indexes in the pattern.matcher() string vary from 0 to 5.

ab34ef
012345

How can the answer be E. 01234456 ?
Shouldn't the answer be the following: 0123445 ?
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried coding the problem and running it with the specified command line.
I got the following : 01234456

I tried running a different command line : java Regex2 "\w*" ab34ef
I got the following : 0ab34ef6
the m.start() method prints 0 and the m.group() method prints ab34ef. After ab34ef are consumed, m.start() prints 6 for the linefeed and m.group() prints the empty string.

It looks as if the matcher() is including the linefeed at the end of the character string.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this...
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another related question. In C++, all "strings" are terminated with a '\0' null character. This char is eight bits, and it consists of eight binary zeros 00000000b. Is it possible that Java does the same thing with String objects? Is it possible that the Matcher.find() method looks at the final null character of the string "ab34ef" ?

Thanks for the citation, by the way. But this quote doesn't explain what's happening. Why does the find() method check the index following the last character ? The find() method has to be looking at something in order to find nothing.

Because a match of zero length is possible, the find() method will check the index following the last character of input.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic