Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Get Formatted Output

 
Ranch Hand
Posts: 181
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print a string values in console I use this statement.


 
 
when it prints in the console it doesn'r formatted.it looks like

Abc[780] abd
Asdbc[23] abd
Adsdbc[090] asd
Assdsddbc[23] abd
Adssdfdbc[090] abd


but I need this way.

Abc[780]    abd
Asdbc[23]    abd
Adsdbc[090]    asd
Assdsddbc[23] abd
Adssdfdbc[090] abd

How can I format my output ?
 
chamini prashakthi
Ranch Hand
Posts: 181
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need it in this way
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't get you.
 
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post what you want as text; photos can be difficult to see and there is a problem viewing yours.

Why are you using the += operator on Strings? For one thing, it can lead to slow performance. For another, are you sure it's doing what you want?
 
Saloon Keeper
Posts: 24811
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm actually not too worried about the string concatenation syntax. A good optimizer can take care of that. But look at this:


Not only does this eliminate any debates about the best way to concatenate strings, it also makes it easier to see what you're creating, since instead of building stuff up piece by piece, you use a template. An alternative to String.format is the messageFormatter, which uses a slightly different syntax:


Which is perhaps easier to read in this particular case, although not as flexible overall.

Actually, I don't recommend putting an explicit newline on a String. Leave that to the (possibly OS-dependent) line output services.

Note that tabs are a data convention and whether or now - and how - they render on a text console depends entirely on the console. Or, for that matter, in any given text editor.
 
Campbell Ritchie
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I'm actually not too worried about the string concatenation syntax. A good optimizer can take care of that.  . . .

You don't notice the performance problem until you are using += several thousand times. Then you end up with garbage collection problems after every few runs of a loop.
But does OP actually want to concatenate Strings like that?

Actually, I don't recommend putting an explicit newline on a String. Leave that to the (possibly OS-dependent) line output services. . . .

I didn't notice that bit. If you use printf or similar, the %n tag is probably the best way to get a line separator.
 
Campbell Ritchie
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nearly an hour ago, I wrote:You don't notice the performance problem until you are using += several thousand times. . . .

So I tried it with 1,000,000 concatenations and it has been chuntering away to itself for about &frac;34; hour and not produced any output yet.
 
Tim Holloway
Saloon Keeper
Posts: 24811
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You don't notice the performance problem until you are using += several thousand times. Then you end up with garbage collection problems after every few runs of a loop.



It's the "=" part that kills you. An optimizer can detect the rest of it at compile time, create an internal StringBuilder and lay it out in a buffer, but the minute you do the assignment, the old string goes to garbage collection (absent any other references). That part is unavoidable, since Strings are immutable. The only cure is never to re-assign in the first place. I'm not assuming here that the sample expression is part of a larger multi-statement concatenation operation - where an explicit StringBuilder would definitely be an improvement.

And I'm being optimistic. Constant folding ("ABC" + "DEF") is a basic part of almost any compiler and Java is no exception, When and if the current crop of JVMs actually does use internal StringBuilders is something I haven't checked, but the option is certainly there. Using a template and a formatter makes it moot, however, since the job is done for you.

I don't care for end-of-line characters when doing internal text processing, since all they do is clutter things up and reduce your options. So unless I'm doing a direct printf to an output device or building a multi-line String instead of something like a String array/list, I prefer to leave them off and let PrintWriter or whatever handle that chore.
 
Campbell Ritchie
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote: . . . It's the "=" part that kills you. . . .

Yes, if I had written it out by hand with 1,000,000 “+” signs in the code, the compiler would have sorted it out and the whole thing would have run so much faster.
 
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see two things you are trying to achieve:
  • Color highlighting. This is console dependent. You'd have to research the escape sequences that your particular console uses. The eclipse console does not support color highlighting.
  • Spacing. Are you required to use tabs for spacing or can you use spaces? You can use printf() with a format string for consistent spacing. You'd have to first concatenate your "Abc" + "[" + "720" + "]" because your spacing will depend on the concatenated width not the individual widths.
  •  
    Tim Holloway
    Saloon Keeper
    Posts: 24811
    172
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is Carey actually trying to colour things? Or is that just an artefact of the IDE, the same way that the colourizers of our message editor highlight based on the code type? Because there's nothing resembling any sort of decorative escapes in the sample, just tabs.

    Tab spacing is, as I said earlier, entirely dependent on the output service. Tab-separated values are a thing in some Unix/Linux utilities, although I prefer pipes instead if CSV is not appropriate. You can't visually tell a tab from one or more spaces in most displays.

    You can do fixed spacing very easily with the printf formatter:

    will print out like this:
     
    Carey Brown
    Saloon Keeper
    Posts: 8920
    76
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Tim Holloway
    Saloon Keeper
    Posts: 24811
    172
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry, I meant Chamini. Lost track of who actually asked the question.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic