Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

The String indent() and stripLeading() - sybex ch 3 #12

 
Greenhorn
Posts: 13
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Unless mistaken, the indent() method adds a line feed (\n) at the end of the string if one is missing.  The stripLeading() only removes the leading spaces and does not remove the added line feed.  Therefore, (with the risk of being pedantic), these two methods do not cancel each other out, as indicated in the answer to question 12 in chapter 4, as shown in the attached image.



Do you think that this sentence may be misleading?  Furthermore, is option D correct, given that the program will print 78 followed by a line feed?

Answer.png
[Thumbnail for Answer.png]
 
Saloon Keeper
Posts: 14091
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have the source material so I don't know what numbers is referring to, but the way the text is phrased implies to me that the two method calls cancel each other out, for that particular value of numbers. It doesn't appear to say that the two method calls always cancel each other out.

If numbers has the same value that you used in your example code snippet, I would agree with you.
 
Albert Attard
Greenhorn
Posts: 13
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, Stephan.

Please find attached the question for your reference.
Question.png
[Thumbnail for Question.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 14091
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albert Attard wrote:Therefore, (with the risk of being pedantic), these two methods do not cancel each other out, as indicated in the answer to question 12 in chapter 4, as shown in the attached image.


Yes, the methods do not cancel each other out exactly, but I would say that that the explanation applies to this specific question, and not to the use of these methods in general.

Do you think that this sentence may be misleading?


Not really, because it explains why there is no space before the '0', and the final newline doesn't really affect which of the options are valid.

Furthermore, is option D correct, given that the program will print 78 followed by a line feed?


Notice that the final statement uses print() and not println(), so it has the exact same effect as if println() had been called but without the additional newline.

Even if the final statement used println(), That would mean that the output would just contain an additional empty line, which is already covered by option E.
 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a note of indent()/stripLeading() as something we could make clearer in the future. It isn't quite wrong so not adding to the errata.

And yes, we used print() instead of println() to avoid the problem with option D.
 
Albert Attard
Greenhorn
Posts: 13
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Stephan and Jeanne, for your replies.

I didn't notice the last print().  Most of the mistakes I made were all like this, where I don't notice something.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic