File ouput formatting

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

File ouput formatting

 
0
  #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:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. /*
  6.  * class LogOrder
  7.  * Allows user to pick from five pieces of clothing to order
  8.  * user enters name and picks clothing and presses order.
  9.  * Stores users order to "order.txt"
  10.  * @author Kimberlie Davis
  11.  * @version 11/20/08
  12.  */
  13.  
  14. public class LogOrder
  15. {
  16. /*
  17.   * main method
  18.   * declares and sets frame as visible
  19.   * @param args not used
  20.   */
  21. public static void main(String args[])
  22. {
  23. EventQueue.invokeLater(
  24. new Runnable()
  25. {
  26. public void run()
  27. {
  28. LogOrderFrame mainFrame = new LogOrderFrame();
  29. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. mainFrame.setVisible(true);
  31. }
  32. });
  33.  
  34. }
  35. }
  36.  
  37. /*
  38.  * LogOrderFrame method
  39.  * sets up frame
  40.  * Declares and adds panel
  41.  */
  42. class LogOrderFrame extends JFrame
  43. {
  44. public LogOrderFrame()
  45. {
  46. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  47. LogOrderPanel panel = new LogOrderPanel();
  48. this.add(panel);
  49. this.setTitle("Log order");
  50. }
  51. private static int DEFAULT_WIDTH = 500;
  52. private static int DEFAULT_HEIGHT = 500;
  53. }
  54.  
  55. /*
  56.  * LogOrderPanel method
  57.  * Layouts and panels
  58.  * Sets up panel with components
  59.  * adds actionListener to button
  60.  */
  61. class LogOrderPanel extends JPanel
  62. {
  63. public LogOrderPanel()
  64. {
  65. //set up layouts
  66. this.setLayout(new BorderLayout());
  67.  
  68. //north
  69. JPanel north = new JPanel(new BorderLayout());
  70. this.add(north, BorderLayout.NORTH);
  71. //north center
  72. JPanel northCenter = new JPanel();
  73. north.add(northCenter, BorderLayout.CENTER);
  74.  
  75.  
  76. //center
  77. JPanel center = new JPanel();
  78. this.add(center, BorderLayout.CENTER);
  79. Box boxLayout = Box.createVerticalBox();
  80. center.add(boxLayout);
  81.  
  82. //set up components
  83. JLabel companyName = new JLabel("Clothing Inc.");
  84.  
  85. JLabel askName = new JLabel("Please enter your name:");
  86. enterName = new JTextField(5);
  87.  
  88. //Array of 5 checkboxes
  89. for(int i = 0; i < 5; i++)
  90. {
  91. check[i] = new JCheckBox();
  92. }
  93. check[0].setText("Jacket");
  94. check[1].setText("Shirt");
  95. check[2].setText("Pants");
  96. check[3].setText("Socks");
  97. check[4].setText("Shoes");
  98.  
  99. JButton order = new JButton("Order");
  100.  
  101. //add components
  102. northCenter.add(askName);
  103. northCenter.add(enterName);
  104. boxLayout.add(check[0]);
  105. boxLayout.add(check[1]);
  106. boxLayout.add(check[2]);
  107. boxLayout.add(check[3]);
  108. boxLayout.add(check[4]);
  109. north.add(companyName, BorderLayout.NORTH);
  110. this.add(order, BorderLayout.SOUTH);
  111.  
  112. ClothingChooser chooseAction = new ClothingChooser();
  113. order.addActionListener(chooseAction);
  114. }
  115.  
  116. /*
  117.   * ClothingChooser class
  118.   * implements ActionListener
  119.   * sets up actions for button
  120.   */
  121. private class ClothingChooser implements ActionListener
  122. {
  123. /*
  124.   * actionPerformed method
  125.   * checks what checkboxes have been selected
  126.   * stores users order to file "order.txt"
  127.   * @param ActionEvent
  128.   */
  129. public void actionPerformed(ActionEvent e)
  130. {
  131.  
  132. try{
  133.  
  134. try
  135. {
  136. String output;
  137. if(writer == null)
  138. writer = new BufferedWriter(new FileWriter("order.txt"));
  139. toStream = new FileWriter(writeTo, true);
  140.  
  141. toStream.write("Order:" + '\n');
  142.  
  143. toStream.write("For: " + enterName.getText() + " " + '\n');
  144.  
  145. if(check[0].isSelected())
  146. {
  147. toStream.write("Jacket \n");
  148. //toStream.write("\n");
  149. }
  150. if(check[1].isSelected())
  151. toStream.write('\t' + "Shirt" + '\n');
  152. if(check[2].isSelected())
  153. toStream.write('\t' + "Pants" + '\n');
  154. if(check[3].isSelected())
  155. toStream.write('\t' + "Socks" + '\n');
  156. if(check[4].isSelected())
  157. toStream.write('\t' + "Shoes" + '\n');
  158. toStream.write('\n');
  159. writer.newLine();
  160.  
  161. JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
  162.  
  163. }
  164. finally
  165. {
  166. toStream.close();
  167. }
  168. }
  169. catch(IOException exception)
  170. {
  171. JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
  172. }
  173.  
  174. }
  175. }
  176.  
  177. //datafields
  178. BufferedWriter writer;
  179. JCheckBox[] check = new JCheckBox[5];
  180. File writeTo = new File("order.txt");;
  181. JTextField enterName;
  182. FileWriter toStream;
  183. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
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: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

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

Re: File ouput formatting

 
0
  #3
Nov 20th, 2008
I tried newLline and couldnt get it to work.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
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: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

 
0
  #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 Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: File ouput formatting

 
0
  #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 Quick reply to this message  
Join Date: May 2007
Posts: 4,483
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: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: File ouput formatting

 
0
  #6
Nov 20th, 2008
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: File ouput formatting

 
0
  #7
Nov 20th, 2008
thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC