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

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

 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!