943,877 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 494
  • Java RSS
Nov 20th, 2008
0

File ouput formatting

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Nov 20th, 2008
1

Re: File ouput formatting

Use the newLine() method to write the line separator, instead of appending '\n' to your text.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 20th, 2008
0

Re: File ouput formatting

I tried newLline and couldnt get it to work.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Nov 20th, 2008
0

Re: File ouput formatting

Why are you trying to use both a FileWriter and a BufferedWriter against the same file? Just use the BufferedWriter.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 20th, 2008
0

Re: File ouput formatting

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!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Nov 20th, 2008
0

Re: File ouput formatting

Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 20th, 2008
0

Re: File ouput formatting

thanks!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help regarding harware interfacing?
Next Thread in Java Forum Timeline: Renaming and deleting text files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC