Basically what you need to know is that this is a search and replace. It searches the input
string for "The" and replaces it with "your". (The case is not corrected.)
The exact way this works is not all that intuitive. I suggest actually coding this and experimenting with it which is what I did to learn it. (I'm using NetBeans which is very helpful because it catches any compile errors right away and it provides links to the API docs.)
Once you play with it for a while you realize that it's just the way it works and the details are not really all that interesting. If you print out the StringBuffer "s" each time through the while loop you'll see what it's doing.
The following will work:
while(m.find()){
m.appendReplacement(s,"your");
System.out.println(s);
}
m.appendTail(s);
System.out.println(s);
This prints out:
your
your Dad and your
your Dad and your Mom
(I don't know why in many examples (including SCJP-Bates/Sierra) they use a separate boolean variable for the results of m.find().)
-----
My question for the community is:
Are appendReplacement() and appendTail() on the exam?
Because I think studying for the exam is hard enough without studying material that will not be included. I have looked in the Sierra/Bates
SCJP book and it never mentions these methods. And in fact says that "you won't have to understand the Matcher methods that facilitate replacing strings in data."
This type of question came up in a Whizlab practice
test, which makes me wonder whether this will come up on the real exam! I spent a considerable amount of time with NetBeans making an example like this to see exactly how it works. While I enjoy learning new methods in
Java, my goal right now is to pass the exam!
Any help would be appreciated - especially a comment from Bert as to whether this is included in the exam. Is there anyway to know exactly what Classes and what Methods are on the exam?
Thanks