Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a print statement text?

Tags:

java

So I was working on my java project and in one part of the program I'm printing out text The text is displayed on the left side However I wanted it be displayed in the middle How many I accomplish this? Is this a newbie question?

Example:

public static void main(String[] args)
{
System.out.println("Hello");
}
like image 882
user2070292 Avatar asked May 18 '13 22:05

user2070292


2 Answers

VERY QUICK answer

You can use the JavaCurses library to do fun things on the console. Read below it's in there.

Before you do though let's answer your entire question in some context

It is a newbie question :) but it's a valid question. So some hints for you:

First question is, how wide is the terminal? (it's counted in number of characters) old terminals had a fixed dimensions of 80 characters and 25 lines;

So as a first step start with the assumption that it's 80 characters wide.

How would you center a string on an 80 character wide terminal screen?

Do you need to worry about the length of the string? How do you position something horizontally? Do you add spaces? Is there a format string you can come up with?

Once you've written a program such that you can give it any string that will display properly on those assumptions (that terminal is 80 characters wide) you can now start worrying about what happens if you are connected to a terminal which is more or less than 80 characters? Or whether or not you are even connected to a terminal. For example if you are not does it make sense to "prettify" your code? probably not.

So question is how do you get all this information?

What you are asking for is the ability to treat the console as a smart teletype (tty) terminal with character-based control capabilities. Teletype terminals of the old can do a lot of fun things.

Some history

Teletype terminals were complicated things and come from the legacy that there were a lots of terminal manufacturers (IBM, DEC, etc.) ... These teletype terminals were developed to solve lots of problems like being able to display content remotely from mainframes and minicomputers.

There were a bunch of terminal standards vt100, vt200, vt220, ansi, that came about at various points in terminal development history and hundreds of proprietary ones along the way.

These terminals could do positioning of cursors and windowing and colors, highlight text, underline etc. but not everyone could do everything. However this was done using "control" characters. ctrl-l is clear screen on ansi and vt terminals, but it may be page feed on something else.

If you wrote a program specific to one it would make no sense elsewhere. So the necessity to make that simple caused a couple of abstraction libraries to developed that would hide away the hideousness.

The first one is called termcap (terminal-capabilities) library, circa 1978, which provided a generic way to deal with terminals on UNIX systems. It could tell a running program of the available capabilities of the terminal (for example the ability to change text color) or to position cursor at a location, or to clear itself etc, and the program would then modify its behavior accordingly.

The second library is called curses, circa 1985 (??) it was developed as part of the BSD system and was used to write games ... One of the most popular versions of this library is the GNU curses library (previously known as ncurses).

On VMS I believe the library is called SMG$ (screen management library).

On with the answer

Any how, so you can use one of these libraries in java to determine whether or not you are working on a proper terminal. There is a library called JavaCurses on source forge that provides this capability to java programs. This will be an exercise in learning how to utilize a new library into your programs and should be exciting.

JavaCurses provides terminal programming capability on both Unix and Windows environments. It will be a fun exercise for you to see if you can use it to play with.

advanced exercise

Another exercise would be to use that same library to see if you can create a program that display nicely on a terminal and also writes out to a text file without the terminal codes;

If you have any issues, post away, I'll help as you go along.

like image 131
Ahmed Masud Avatar answered Oct 02 '22 09:10

Ahmed Masud


If you have a definite line length, apache commons StringUtils.center will easily do the job. However, you have to add that library. javadoc

like image 35
Laksitha Ranasingha Avatar answered Oct 02 '22 08:10

Laksitha Ranasingha