• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Proper output alignment of Columns in Text File

hello guys and gals, i'm outputting a lot of data(several tables) to a text file but i'd like an alighment of some sort so that the data can be easily read.

i've outputted three tables to one txt file(three structure class types with several fields) the first structure has 3 fields and i output to file all the fields but the spacing is horrible.

the second table has 5 fields and the last table has 4 fields.

anyone know how i can output my tables properly in java?

**TO OUTPUT DATA TO A TXTFILE, i am using PRINTWRITER.**

example
one    two    three      Hello
four    five    six          Bye
0
fuzzyfluid
Asked:
fuzzyfluid
1 Solution
 
sudhakar_koundinyaCommented:
Assuming your max field length is 30

here is an idea

import java.util.*;
import java.io.*;
class TestJavaPrint
{
      private static String process(String str)
      {
            int n=30;
            n=30-str.length();
            String str1="";
            for(int i=0;i<n;i++)
            {
                  str1=str1+" ";
            }
            str=str+str1;
            return str;
      }
      public static void main(String[] args) throws Exception
      {
            String[]array0={"Murty","Kameswari","Sudhakar","Prabhakar"};
            String[]array1={"Sudhakar","Lakshmi","","Niharika"};
            String[]array2={"Prabhakar","","","Bujji"};
            Vector v=new Vector();
           v.add(array0);
            v.add(array1);
            v.add(array2);
             PrintWriter pw=new PrintWriter(new FileOutputStream("out.txt"));
            for(int i=0;i<v.size();i++)
            {
                  String[] array=(String[]) v.get(i);
                  StringBuffer sb=new StringBuffer();
                  for(int j=0;j<array.length;j++)
                  {
                        String str=process(array[j]);
                        sb.append(str);
                  }
                  pw.println(sb);
            }
            pw.close();
      }
}


0
 
sudhakar_koundinyaCommented:
But you may not see the expected output if you try open the out.txt in GUI base text editors such as notepad,ediplus

The reason is each text editor maintains some font to show the file.  As you know font is based on size of character and other attributes, you won't get the expected output.

Anyhow you can view the expectedoutput  in dos based editor. Becuase it won't maintain the font based attributes to show the text
Example:

c:\>edit out.txt

And also if you try to print the text, you get the expected output
example
c:\>type out.txt >PRN

Regards
Sudhakar




0
 
CEHJCommented:
If you're using 1.5 you can do

out.printf("%-10s%-10s%-10s%-10s\n", "one", "two", "three", "Hello");

which will control the spacing in columns, as long as you view it with a monospaced font
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fuzzyfluidAuthor Commented:
i believe im using java 1.4.2

i would have loved to be able to use something as simple as out.printf :(
0
 
CEHJCommented:
>>i believe im using java 1.4.2

Ah well, update your jdk ;-)
0
 
objectsCommented:
> Ah well, update your jdk ;-)

Just updating your jdk would not really help, you would still require 1.5 to run it.

> i would have loved to be able to use something as simple as out.printf :(

grab a 3rd party implementation and use that, there are lots available:

http://www.braju.com/
http://sharkysoft.com/software/java/lava3/printf/
http://www.eli.sdsu.edu/java-SDSU/
http://www.acme.com/java/software/Acme.Fmt.html
http://www.ibiblio.org/javafaq/formatter/
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now