Below is an address book program I am working on. The way it stands now, when the data is entered and writes to a text file it writes the line of data like,

John Doe 1234 Help Me Avenue Dallas Texas 98956
Johnathan Doe 2345 Derby Lane Omaha Nebraska 45678

What I would like it to do is keep all the data evenly spaced and with headings for each column...

First Name   Last Name         Address             City      State       Zipcode

John         Smith       1234   Help Me Avenue     Dallas    Texas       98956
Johnathan    Doe         2345   Derby Lane         Omaha     Nebraska    45678

All Help will be greatly appreciated. Thanks!

//********************************************************
// Author: W Lewis, M Shepard
//   
//********************************************************

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.JTextField.*;
import javax.swing.JOptionPane.*;
import java.awt.*;
import java.awt.event.*;

public class AddressBookProgram extends JFrame
{


   //public static void main(String[] args) throws FileNotFoundException


   private JLabel blankL, fnameL, lnameL, addressL, cityL, stateL, zipL, hphoneL, mphoneL; 
    //left column of GUI component for field names

   private JTextField blankTF, fnameTF, lnameTF, addressTF, cityTF, stateTF, zipTF, hphoneTF, mphoneTF;
    //right column of GUI component for entering information in the fields

   private JButton resetB, addB, exitB; //Buttons listed on GUI component


    private ResetButtonHandler rbHandler;
    private AddButtonHandler abHandler;
   private ExitButtonHandler ebHandler;
    //methods objects for submit buttons on GUI

   private static final int WIDTH = 600;
   private static final int HEIGHT = 400;
    //height and width of GUI component

   public AddressBookProgram()
   {


    JMenuBar menuBar = new JMenuBar();

// Add the menubar to the frame
    setJMenuBar(menuBar);

// Define and add two drop down menu to the menubar
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("About");
    menuBar.add(fileMenu);
    menuBar.add(editMenu);

// Create and add simple menu item to one of the drop down menu
    JMenuItem newAction = new JMenuItem("New");
    JMenuItem openAction = new JMenuItem("Open");
    JMenuItem saveAction = new JMenuItem("Save");

    JMenuItem closeAction = new JMenuItem("Close");

    fileMenu.add(newAction);
    fileMenu.add(openAction);
    fileMenu.add (saveAction);
     fileMenu.addSeparator();
     fileMenu.add(closeAction);

             //Create the labels, right justified
      fnameL = new JLabel("First Name: ",SwingConstants.RIGHT);
      lnameL = new JLabel("Last Name: ",SwingConstants.RIGHT);
      addressL = new JLabel("Address: ", SwingConstants.RIGHT);
      cityL = new JLabel("City: ", SwingConstants.RIGHT);
      stateL = new JLabel("State: ", SwingConstants.RIGHT);
      zipL = new JLabel("Zip Code: ", SwingConstants.RIGHT);
      hphoneL = new JLabel("Home Phone: ", SwingConstants.RIGHT);
       mphoneL = new JLabel("Mobile Phone: ", SwingConstants.RIGHT);


      //Create the text fields
      fnameTF = new JTextField(10);
      lnameTF = new JTextField(10);
      addressTF = new JTextField(40);
      cityTF = new JTextField(10);
      stateTF = new JTextField(10);
      zipTF = new JTextField(5);
        hphoneTF = new JTextField(10);
        mphoneTF = new JTextField(10);


                //Create Reset Button to clear text in field and re-enter information
        resetB = new JButton("Reset");
        rbHandler = new ResetButtonHandler();
        resetB.addActionListener(rbHandler);

                //Create Add to File Button which will print all informatio to a file
        addB = new JButton("Add to File");
        abHandler = new AddButtonHandler();
        addB.addActionListener(abHandler);

             //Create Exit Button closes the GUI screen when complete
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);

             //Set the title of the window
      setTitle("Address Book");

             //Get the container
      Container pane = getContentPane();


             //Set the layout of rows and columns

      pane.setLayout(new FlowLayout(1,5,10));



             //Place the components in the pane
      pane.add(fnameL);
      pane.add(fnameTF);
      pane.add(lnameL);
      pane.add(lnameTF);
      pane.add(addressL);
      pane.add(addressTF);
      pane.add(cityL);
      pane.add(cityTF);
        pane.add(stateL);
        pane.add(stateTF);
      pane.add(zipL);
      pane.add(zipTF);
      pane.add(hphoneL);
      pane.add(hphoneTF);
        pane.add(mphoneL);
        pane.add(mphoneTF);



        pane.add(addB);
        pane.add(resetB);
      pane.add(exitB);

             //Set the size of the window and display it
      setSize(WIDTH, HEIGHT);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
   }


    private class ResetButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        //resets JTextFields
        fnameTF.setText("");
        lnameTF.setText("");
        addressTF.setText("");
        cityTF.setText("");
        stateTF.setText("");
        zipTF.setText("");
        hphoneTF.setText("");
        mphoneTF.setText("");
    }




    }
    //Add to File code
    private class AddButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (fnameTF.getText().length() == 0)
    {
    JOptionPane.showMessageDialog(null, "First Name is required ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

        fnameTF.requestFocusInWindow();
    }   

    else if(lnameTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "Last Name is required ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    lnameTF.requestFocusInWindow();
    }

    else if(addressTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "Address is required ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    addressTF.requestFocusInWindow();
    }

    else if(cityTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "City is required ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    cityTF.requestFocusInWindow();
    }

    else if(stateTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "State is reqired ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    stateTF.requestFocusInWindow();
    }

    else if(zipTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "Zip Code is reqired ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    zipTF.requestFocusInWindow();
    }

    else if(hphoneTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "Home Phone is reqired ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    hphoneTF.requestFocusInWindow();
    }

    else if(mphoneTF.getText().length() == 0)
    {

    JOptionPane.showMessageDialog(null, "Mobile Phone is reqired ",

        "ERROR",JOptionPane.ERROR_MESSAGE);

    mphoneTF.requestFocusInWindow();
    }

        // Stream to write file
        FileOutputStream fout;      

        try
    {       

    boolean exists = (new File("myfile.txt")).exists();
    if (exists) 
     {
    // File or directory exists
     // Print a line of text

    FileOutputStream ps = new FileOutputStream("myfile.txt", true);


     new PrintStream(ps).print ("" + fnameTF.getText() + " " + lnameTF.getText()); 
     new PrintStream(ps).print (", " + addressTF.getText());
     new PrintStream(ps).print (", " + cityTF.getText());
     new PrintStream(ps).print (", " + stateTF.getText());
     new PrintStream(ps).print (", " + zipTF.getText());
    new PrintStream(ps).print (", " + hphoneTF.getText());
    new PrintStream(ps).print (", " + mphoneTF.getText());
    new PrintStream(ps).println ();

         ps.close();        

     fnameTF.setText("");
     lnameTF.setText("");
     addressTF.setText("");
     cityTF.setText("");
     stateTF.setText("");
     zipTF.setText("");
     hphoneTF.setText("");
     mphoneTF.setText("");

     } 
     else 
     {
    // File or directory does not exist
     // Open an output stream
    fout = new FileOutputStream ("myfile.txt");

    // Print a line of text
    new PrintStream(fout).print ("" + fnameTF.getText() + " " + lnameTF.getText()); 
     new PrintStream(fout).print (", " + addressTF.getText());
     new PrintStream(fout).print (", " + cityTF.getText());
     new PrintStream(fout).print (", " + stateTF.getText());
    new PrintStream(fout).print (", " + zipTF.getText());
    new PrintStream(fout).print (", " + hphoneTF.getText());
    new PrintStream(fout).print (", " + mphoneTF.getText());
    new PrintStream(fout).println ();

     fnameTF.setText("");
     lnameTF.setText("");
     addressTF.setText("");
     cityTF.setText("");
     stateTF.setText("");
     zipTF.setText("");
     hphoneTF.setText("");
     mphoneTF.setText("");


    // Close the output stream
    fout.close();       
        }
        }
        catch (IOException f)
        {
     System.err.println ("Unable to write to file");
     System.exit(-1);
        }

        }
    }

   private class ExitButtonHandler implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           System.exit(0);
       }
   }


   public static void main(String[] args) throws FileNotFoundException
   {



        try 
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 
catch (Exception e) 
{ 
}
   AddressBookProgram rectObject = new AddressBookProgram();



   }

}

the String.format() method can definitly save you a lot of manual work with this.

read this article,it shows exactly what you want to do! :)

just because i realize the article above doesn't explain what is being done much , heres a little quote to clarify whats going on :

1.3. width specifier

An optional "width specifier," if present, indicates the field with, or the minimum number of characters in the output that the formatted argument will span. If the string representation of the value does not fill the minimum length, the field will be left-padded with spaces. If the converted value exceeds the minimum length, however, the converted result will not be truncated.

Example: If the format specifier is "%6d", and the supplied int argument is 52, then the output will be "____52" (four spaces on the left).

If no width is specified, then there is no minimum size for the converted result. The field will be only as large as necessary to display the result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.