• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA 8 e-book: String replace contradiction?

 
Chris Kuyler
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm planning to take the OCA 8 exam in a few months and I've already downloaded the OCA 8 ebook from O'Reilly to prepare for it.

One thing I'm still unsure of. On page 110 it gives the following examples for the String replace function:



So this would seemingly demonstrate that for the replace method, all occurrences are replaced (i.e. both a's are capitalized after the operation).

However, on page 111 there is the following example:



So in this case, only the first letter a is replaced, and not the second one. Am I missing something here, or is there a typo somewhere?

Thanks in advance!

Chris

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

First of all, a warm welcome to CodeRanch!

Chris Kuyler wrote:Am I missing something here, or is there a typo somewhere?

Let's see what the javadoc of String.replace tells us:
replace(char oldChar, char newChar) wrote:Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

And this code definitely shows it's a typo on page 111:


Hope it helps!
Kind regards,
Roel
 
Paul Clapham
Sheriff
Posts: 21557
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Kuyler wrote:On page 110 it gives the following examples for the String replace function


What you are missing is that you think that there are two examples for one method there. (By the way the Java name is "method", not "function".) But there are two different methods in that example. The first method takes two char values as its parameters and the second method takes two String values as its parameters. And the two methods work differently; have a look in the API document to read about how they actually work.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And the two methods work differently; have a look in the API document to read about how they actually work.

Both methods will replace all occurences according to javadoc:
replace(char oldChar, char newChar) wrote:Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement) wrote:Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".


And illustrated in this little code snippet as well:


Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Welcome to CodeRanch! Thank you for pointing out the errata. Until the publisher starts their errata page, I'm keeping a list here. I want to congratulate you for being the first person I don't know in the real word who found/posted an error. (The other two are from Scott and one of my friends.)

ps - It's interesting that it is possible to download an e-book published by Wiley on O'Reilly given that they are competing publishing houses.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris! We really do appreciate it. The book goes through a lot of revisions and sometimes errors creep in late, sometimes even after Jeanne and I have signed off on something. These things happen and were glad we have thoughtful readers to help find these mistakes. Keep up the good work!
 
Chris Kuyler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification everyone, and for the welcome!

Jeanne, I appreciate you having given this topic credit on your errata page. I'm surprised no one noticed on the forum yet, but I guess most of those going for OCA 8 are waiting for the printed version (or even still just opting for OCA 7). The reason I chose the e-book was because I liked being able to study from anywhere, without having to carry a big book around.

I'll of course be happy to post again if I notice something else.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Kuyler wrote:I'm surprised no one noticed on the forum yet, but I guess most of those going for OCA 8 are waiting for the printed version (or even still just opting for OCA 7).

People who want to take on OCP certification after passing OCA don't have another option then OCA 7, because the OCP 8 beta isn't announced yet. And after the beta ends, you'll have to wait another 11 weeks before you know your score. And you can't combine different version certifications. So I think currently the OCA 7 is still the more popular one.
 
Chris Kuyler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Chris Kuyler wrote:I'm surprised no one noticed on the forum yet, but I guess most of those going for OCA 8 are waiting for the printed version (or even still just opting for OCA 7).

People who want to take on OCP certification after passing OCA don't have another option then OCA 7, because the OCP 8 beta isn't announced yet. And after the beta ends, you'll have to wait another 11 weeks before you know your score. And you can't combine different version certifications. So I think currently the OCA 7 is still the more popular one.


Yeah I guess that makes sense. I want to get OCA certified within a few months. I don't mind waiting another few months after that for the OCP 8.
 
Chris Kuyler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Until the publisher starts their errata page, I'm keeping a list here. I want to congratulate you for being the first person I don't know in the real word who found/posted an error. (The other two are from Scott and one of my friends.)

I may have another small typo for your list. A stray L on page 124:



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Kuyler wrote:I may have another small typo for your list. A stray L on page 124:

Thanks. Added it to the list as well. This one is interesting. It was right when I first wrote it. Then I must have hit the L key when replying to comments from the editor. And then there it was.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic