• 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

Trying to understand text blocks and indent() method?

 
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 4. Core APIs and indent().

Would be grateful if someone can confirm if I understood my test example based on a content from book.



Their explanation:


Line 16 counts the six characters in block, which are the three letters, the blank space before b, and the \n after a and b. Line 17, we ask Java to add a single blank space to each of the three lines in block.
However, the output says we added 4 characters rather than 3 since the length went from 6 to 10. This mysterious additional character is thanks to the line termination normalization. Since the text block doesn’t have a line break at the end, indent() adds one!



My text block:



My explanation why length is 14.

We will have six rows, as follow:
Row1: whitespace + a+\n =>>> 3 char total.  \n is added manually, so text block doesn't add another \n
Row2: whitespace +\n  =>>> 2  char total.  whitespace is added because of indent() and \n is added implicitly by the Java/text block
Row3: whitespace + whitespace + b + \n   =>>> 4 char total.  \n is added manually, so text block doesn't add another \n
Row4: whitespace + \n  =>>> 2 char total.  whitespace is added because of indent() and \n is added implicitly by the Java/text block
Row5: whitespace + c + \n  =>>> 3  char total. Since string is ending with \n indent() doesn't add it
Row6: empty =>>> 0  char total.
 
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I make that nine characters not 14. Remember that "common" leading whitespace is removed. Your literal newlines will be followed by the invisible newlines, so you'll have the following characters:

a, newline, newline, space, b, newline, newline, c, newline

That makes nine. Easy enough to check in jshell:



Now, if there's something funky, perhaps tab characters, in your whitespace, maybe that could explain what's going on with you getting 14, I'm not sure. My editors all replace tabs with regular spaces
Note that *trailing* whitespace will be trimmed, so it's not that.

Hope this helps.
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I placed this code in a simple file and compile it with java command. The value is 14. Sorry In my example I was referring to length of variable 'test' with indent(1).

Is my way of counting still correct?
 
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike!

Simon is right the line initially was 9 chars length:

Like that (space denoted as underscore):

a\n\n_b\n\nc\n

Once you indented, each line got an extra space:

_a\n_\n__b\n_\n_c\n

Now the length becomes 14.

HTH,
MZ
 
Simon Roberts
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Gosling wrote:Sorry In my example I was referring to length of variable 'test' with indent(1).



Apologies, I missed the indent part.

Of course, the answer is what the code prints when it runs, and I think Mikalai has pinned that down.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic