?
Solved

Proper output alignment of Columns in Text File

Posted on 2005-03-05
6
Medium Priority
?
309 Views
Last Modified: 2010-03-31
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
Comment
Question by:fuzzyfluid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 200 total points
ID: 13465161
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13465178
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13465847
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!

 

Author Comment

by:fuzzyfluid
ID: 13467218
i believe im using java 1.4.2

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

Expert Comment

by:CEHJ
ID: 13467708
>>i believe im using java 1.4.2

Ah well, update your jdk ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13468196
> 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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question