java - How to center a print statement text? -


so working on java project , in 1 part of program i'm printing out text text displayed on left side wanted displayed in middle how many accomplish this? newbie question?

example:

public static void main(string[] args) { system.out.println("hello"); } 

very quick answer

you can use javacurses library fun things on console. read below it's in there.

before though let's answer entire question in context

it newbie question :) it's valid question. hints you:

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

so first step start assumption it's 80 characters wide.

how center string on 80 character wide terminal screen?

do need worry length of string? how position horizontally? add spaces? there format string can come with?

once you've written program such can give string display on assumptions (that terminal 80 characters wide) can start worrying happens if connected terminal more or less 80 characters? or whether or not connected terminal. example if not make sense "prettify" code? not.

so question how information?

what asking ability treat console smart teletype (tty) terminal character-based control capabilities. teletype terminals of old can lot of fun things.

some history

teletype terminals complicated things , come legacy there lots of terminal manufacturers (ibm, dec, etc.) ... these teletype terminals developed solve lots of problems being able display content remotely mainframes , minicomputers.

there bunch of terminal standards vt100, vt200, vt220, ansi, came @ various points in terminal development history , hundreds of proprietary ones along way.

these terminals positioning of cursors , windowing , colors, highlight text, underline etc. not everything. done using "control" characters. ctrl-l clear screen on ansi , vt terminals, may page feed on else.

if wrote program specific 1 make no sense elsewhere. necessity make simple caused couple of abstraction libraries developed hide away hideousness.

the first 1 called termcap (terminal-capabilities) library, circa 1978, provided generic way deal terminals on unix systems. tell running program of available capabilities of terminal (for example ability change text color) or position cursor @ location, or clear etc, , program modify behavior accordingly.

the second library called curses, circa 1985 (??) developed part of bsd system , used write games ... 1 of popular versions of library gnu curses library (previously known ncurses).

on vms believe library called smg$ (screen management library).

on answer

any how, can use 1 of these libraries in java determine whether or not working on proper terminal. there library called javacurses on source forge provides capability java programs. exercise in learning how utilize new library programs , should exciting.

javacurses provides terminal programming capability on both unix , windows environments. fun exercise see if can use play with.

advanced exercise

another exercise use same library see if can create program display nicely on terminal , writes out text file without terminal codes;

if have issues, post away, i'll go along.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -