RSS Forums RSS

File ouput formatting

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 107
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

File ouput formatting

  #1  
Nov 20th, 2008
I have this program all working, except in how the text looks in the file. The text is supposed to be all on different lines, but its all on the same.

Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/*
 * class LogOrder
 * Allows user to pick from five pieces of clothing to order
 * user enters name and picks clothing and presses order.
 * Stores users order to "order.txt"
 * @author Kimberlie Davis
 * @version 11/20/08
 */

public class LogOrder
{
    /*
     * main method
     * declares and sets frame as visible
     * @param args not used
     */
    public static void main(String args[])
    {
      EventQueue.invokeLater(
        new Runnable()
        {
            public void run()
            {
                LogOrderFrame mainFrame = new LogOrderFrame();
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainFrame.setVisible(true);
            }
        });

    }
}

/*
 * LogOrderFrame method
 * sets up frame
 * Declares and adds panel
 */
class LogOrderFrame extends JFrame
{
    public LogOrderFrame()
    {
     this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
     LogOrderPanel panel = new LogOrderPanel();
     this.add(panel);
     this.setTitle("Log order");
    }
    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;
}

/*
 * LogOrderPanel method
 * Layouts and panels
 * Sets up panel with components
 * adds actionListener to button
 */
class LogOrderPanel extends JPanel
{
    public LogOrderPanel()
    {
        //set up layouts
        this.setLayout(new BorderLayout());
        
        //north
        JPanel north = new JPanel(new BorderLayout());
        this.add(north, BorderLayout.NORTH);
        //north center
        JPanel northCenter = new JPanel();
        north.add(northCenter, BorderLayout.CENTER);
        
        
        //center
        JPanel center = new JPanel();
        this.add(center, BorderLayout.CENTER);
        Box boxLayout = Box.createVerticalBox();
        center.add(boxLayout);
        
        //set up components
        JLabel companyName = new JLabel("Clothing Inc.");
        
        JLabel askName = new JLabel("Please enter your name:");
        enterName = new JTextField(5);
        
        //Array of 5 checkboxes
        for(int i = 0; i < 5; i++)
        {
            check[i] = new JCheckBox();
        }
        check[0].setText("Jacket");
        check[1].setText("Shirt");
        check[2].setText("Pants");
        check[3].setText("Socks");
        check[4].setText("Shoes");
        
        JButton order = new JButton("Order");
       
        //add components
        northCenter.add(askName);
        northCenter.add(enterName);
        boxLayout.add(check[0]);
        boxLayout.add(check[1]);
        boxLayout.add(check[2]);
        boxLayout.add(check[3]);
        boxLayout.add(check[4]);
        north.add(companyName, BorderLayout.NORTH);
        this.add(order, BorderLayout.SOUTH);
        
        ClothingChooser chooseAction = new ClothingChooser();
        order.addActionListener(chooseAction);
    }
    
    /*
     * ClothingChooser class
     * implements ActionListener
     * sets up actions for button
     */
    private class ClothingChooser implements ActionListener
    {
        /*
         * actionPerformed method
         * checks what checkboxes have been selected
         * stores users order to file "order.txt"
         * @param ActionEvent
         */
        public void actionPerformed(ActionEvent e)
        {
            
         try{
        
           try
           {
               String output;
                if(writer == null)
                writer = new BufferedWriter(new FileWriter("order.txt"));
                toStream = new FileWriter(writeTo, true);
                
                toStream.write("Order:" + '\n');
            
                toStream.write("For: " + enterName.getText() + " " + '\n');
         
                if(check[0].isSelected())
                {
                    toStream.write("Jacket \n");
                    //toStream.write("\n");
                }
                if(check[1].isSelected())
                    toStream.write('\t' + "Shirt" + '\n');
                if(check[2].isSelected())
                    toStream.write('\t' + "Pants" + '\n');
                if(check[3].isSelected())
                    toStream.write('\t' + "Socks" + '\n');
                if(check[4].isSelected())
                    toStream.write('\t' + "Shoes" + '\n');
                toStream.write('\n');
                writer.newLine();
       
                JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
             
           }
           finally
           {
             toStream.close();
           }
         }
         catch(IOException exception)
         {
             JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
         }
         
        }
 }
    
    //datafields
    BufferedWriter writer;
    JCheckBox[] check = new JCheckBox[5];
    File writeTo = new File("order.txt");;
    JTextField enterName;
    FileWriter toStream;
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

  #2  
Nov 20th, 2008
Use the newLine() method to write the line separator, instead of appending '\n' to your text.
Reply With Quote  
Posts: 107
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: File ouput formatting

  #3  
Nov 20th, 2008
I tried newLline and couldnt get it to work.
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

  #4  
Nov 20th, 2008
Why are you trying to use both a FileWriter and a BufferedWriter against the same file? Just use the BufferedWriter.
Reply With Quote  
Posts: 107
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: File ouput formatting

  #5  
Nov 20th, 2008
I cant get the BufferedWriter to write. Thats what I used originally but nothing would show up in the file, so I used FileWriter. Could you give me an example of how to write with BufferedWriter?
thanks!
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

  #6  
Nov 20th, 2008
Reply With Quote  
Posts: 107
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: File ouput formatting

  #7  
Nov 20th, 2008
thanks!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Java Forum
Views: 366 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:03 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC