LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2004, 07:35 AM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
align columns of strings and explain '\t' (java)


hi
I have an array of objects. Each object contains 4 string variables and a method to display these variables. The method looks like this

Code:
System.out.println(string1 + "\t" + string2 + "\t" + string3 + "\t" + string4);
i call the method with a for loop in another class, for all the objects in the array call this method. As a result i have many lines of text written to the console but they are not aligned propely and i want them in straight columns.

My questions are:
1. 'why is is that the '\t' does not create an equal space between each string, sometimes its 2 chars, sometimes its 5 or 6?

2. Even if the tab made a consistently sized space my columns would not be aligned propely as not all the strings are the same length. Is there a built in method which would say . .

-write first string
-write second string on same line but 10 chars from the left
-write third string on same line but 20 chars from left

im trying to make a method of my own but its a lot of code so really im looking for an economical shortcut.

thanks

Last edited by dave bean; 01-10-2004 at 07:41 AM.
 
Old 01-10-2004, 07:41 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
A tab generally will advance the cursor to the next tab space. A common tab spacing is 8, so if you were to do something like so...

Code:
System.out.println("012345678901234567890");
System.out.println("a\tb");
System.out.println("abc\tdef");
System.out.println("abcdefghijklm\tnop");
You get a result similar to the following if the tab columns are every 8th place..

Code:
012345678901234567890
a       b
abc     def
abcdefghijklm   nop
Note that a single tab will add white space up to the next tab column. (In this case the tab alignment is every 8 characters.)

So... if you want to make sure everything is alligned by using tabs, you'll have to know at what tab space each column is at, and how long each column you print out is, so that you know how many tabs you need to use in order to align the next column on the appropriate tab.

So for example, if you wanted to align the above, you'd need to use this instead.

Code:
System.out.println("012345678901234567890");
System.out.println("a\t\tb");
System.out.println("abc\t\tdef");
System.out.println("abcdefghijklm\tnop");


Results:
012345678901234567890
a               b
abc             def
abcdefghijklm   nop

Last edited by deiussum; 01-10-2004 at 07:50 PM.
 
Old 01-10-2004, 09:17 PM   #3
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
ok thanks deiussum

so would you say the fastest way to do this is to check the number of characters in the column before printing out, and then set the number of tabs accordingly for each line ?
 
Old 01-10-2004, 10:55 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
That is one option. If you were using C/C++, I would say that using a library such as ncurses might give you a bit more control of formatting where you want to write everything w/o using tabs. I don't know of any libraries offhand that will do that for Java, but I'm guessing they probably exist somewhere.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Java printing - problem with large strings Andy@DP Programming 2 08-03-2004 02:23 PM
Unable to align parition properly alexellis Fedora - Installation 4 07-05-2004 10:11 AM
Java Strings jbstew32 Programming 3 02-15-2004 12:08 PM
align cartridges nielchiano Linux - Hardware 3 02-03-2004 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration