Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Preformatting a large block of text  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me if this is a dumb question, haven't done Java in a year now.

I have a large block of text I want to pre-format (e.g. in the source code), and print when needed. In other words, something like


The poem is by Joyce Kilmer.

When the user enters the -h option on the command line I want to print something pre-formatted like the above. What's the best way to do that? I suspect it's related to multi-language support, but as I only speak english I won't be translating it.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Before worrying about code, can you explain how you know what to indent in English?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to be pedantic, but does it matter?  I want to display a block of text in english that is formatted a certain way.  Plan right now is to put each line into an array, then loop over the array to display the text. 

I'm asking if there is a better way, maybe a Java library that handles this sort of thing.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Jeanne gave a reasonable question, I understood similarly the difficulty of this exercise it might be, but it seems you mean something different probably.

=Jim Venolia wrote:I want to display a block of text in english that is formatted a certain way.
Is the text known in advance (at compile time)? Is the formatting rules known in advance (at compile time)?

Jim Venolia wrote:I'm asking if there is a better way, maybe a Java library that handles this sort of thing.
You failed to explain clearly what do you want to do. To handle what sort of thing?
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
formatted a certain way
And what way would that be?

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's the C way.  But what if I run my program on a system that used CRLF instead of just CR?  println() takes care of that for me.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Yeah, that's the C way.  But what if I run my program on a system that used CRLF instead of just CR?  println() takes care of that for me.

 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I figure you don't want Carey posted so I was trying to draw out the problem you are trying to solve.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Right. I figure you don't want Carey posted so I was trying to draw out the problem you are trying to solve.

Right. I figured that Jim didn't want what I posted either but was trying to make a point that this was as good as he was going to get without more info.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Jim Venolia wrote:Yeah, that's the C way.  But what if I run my program on a system that used CRLF instead of just CR?  println() takes care of that for me.

Or
System.out.printf("...%n...%n", ...);
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, no elegant way.  Put my strings into an array and loop over them via println.
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:.. Put my strings into an array and loop over them via println.

If your block of text gets too lengthy, you might want to put it in a text file resource, and read it in when required.
 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ron. Never put big chunks of text in source code, especially if they need to be pre-formatted. Put it in a resource file, and then you can loop through the lines:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!