Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to output an array of strings in neat columns the number of which depends on console width?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Cross-post from https://stackoverflow.com/questions/33185573/is-there-a-way-to-make-java-output-an-array-of-strings-in-neat-columns-the-numbe)

So I'm writing an application that would output an array of strings, but I would like those strings to be outputted in a very neat columns. The number of those columns depends on the width of the opened Windows console window, as well as the width of the strings themselves, naturally.

An example of what I want would be running in the Windows command line:



If you go into your console properties and resize the window, you will notice that the output will always perfectly fit the width of your window.

QUESTION: Is there a native JDK way to do this "easily"? Alternatively, is there an open source library that can do this? If the answer is "no" to both, would you kindly give some pointers on how *you* would approach this problem?

Note: I'm only interested in the Windows implementation of this, no need for cross-platform solutions.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first thought is that if you only want to do this on a windows platform, then don't use java. This would most likely be easier using .net.

One very important point nobody teaches about programming is that you should always use the right tool for the right job. Given the requirements you state, java does not seem like the correct choice to me.

 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:My first thought is that if you only want to do this on a windows platform, then don't use java. This would most likely be easier using .net.

One very important point nobody teaches about programming is that you should always use the right tool for the right job. Given the requirements you state, java does not seem like the correct choice to me.



Perhaps there's some library that would allow me to invoke the particular functionality I'm looking for?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jeffersonian wrote:QUESTION: Is there a native JDK way to do this "easily"? Alternatively, is there an open source library that can do this?


Simple answer: dunno.

If the answer is "no" to both, would you kindly give some pointers on how *you* would approach this problem?


There are two possibilities here:
1. 'dir' simply spews out long lines and the console just wraps them.
2. 'dir' has some way of "knowing" what the line width is and formats its own lines accordingly.

Assuming the latter, there may be a command that allows you work out what the width is - and if that's the case, just pass it to your java program when you invoke it.

HIH

Winston
 
Saloon Keeper
Posts: 9565
79
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
You would need to know the width of the console in characters and the maximum width of a String. Divide the former by the latter and will give you the column width. For each string create a temporary StringBuffer and append spaces until it arrives at the desired width of the column.
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are going to create a String Buffer you might as well use it to collect all the output and print it once only. That will reduce the performance overhead of multiple print calls.

You can easily use the %s tag:-
System.out.printf("%" + consoleWidth / 4 + "s%" + consoleWidth / 2 + "s%n",
    myText, otherText);


The problem is how to find the width of the terminal/console. It usually defaults to 80.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic