943,660 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 477
  • Java RSS
Dec 2nd, 2008
0

File I/0

Expand Post »
This program is all working except when a user chooses the same file as before it needs to show both orders in the file but it is only showing the latest one.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. /**
  7.  * Program that allows user to select a meal.
  8.  * Includes sides, drink, and main entree
  9.  * Writes users order to user specified file.
  10.  * @author Kimberlie
  11.  * @version 12/4/08
  12.  */
  13. public class MealBuilder {
  14.  
  15. /*
  16.   * Main
  17.   * Sets up frame
  18.   * @param args not used
  19.   */
  20. public static void main(String[] args)
  21. {
  22. EventQueue.invokeLater(
  23. new Runnable()
  24. {
  25. public void run()
  26. {
  27. MealBuilderFrame frame = new MealBuilderFrame();
  28. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. frame.setVisible(true);
  30. }
  31. });
  32. }
  33.  
  34. }
  35.  
  36. /*
  37.  * MealBuilderFrame class
  38.  * extends JFrame
  39.  * Sets up frame, sets size and title
  40.  * Declares and adds panel
  41.  */
  42. class MealBuilderFrame extends JFrame
  43. {
  44. /*
  45.   * MealBuilderFrame constructor
  46.   * @param none
  47.   */
  48. public MealBuilderFrame()
  49. {
  50. this.setTitle("Meal Builder");
  51. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  52.  
  53. MealBuilderPanel panel = new MealBuilderPanel();
  54. this.add(panel);
  55. }
  56.  
  57. final static int DEFAULT_WIDTH = 550;
  58. final static int DEFAULT_HEIGHT = 400;
  59. }
  60.  
  61. /*
  62.  * MealBuilderPanel class
  63.  * extends JPanel
  64.  * Sets up panel with components
  65.  */
  66. class MealBuilderPanel extends JPanel
  67. {
  68. /*
  69.   * MealBuilderPanel constructor
  70.   * @param none
  71.   */
  72. public MealBuilderPanel()
  73. {
  74. this.setLayout(new BorderLayout());
  75.  
  76. //ButtonGroup for radio buttons
  77. ButtonGroup entree = new ButtonGroup();
  78. ButtonGroup drinkGroup = new ButtonGroup();
  79.  
  80. //North
  81. this.add(new JLabel("Server: Kimberlie Davis"), BorderLayout.NORTH);
  82.  
  83. //West
  84. this.add(new JLabel(" "), BorderLayout.WEST);
  85.  
  86. //Center
  87. JPanel centerPanel = new JPanel(new GridLayout(1,4));
  88. this.add(centerPanel, BorderLayout.CENTER);
  89.  
  90. //south
  91. JPanel southPanel = new JPanel();
  92. this.add(southPanel, BorderLayout.SOUTH);
  93. JButton submit = new JButton("Order");
  94. southPanel.add(submit);
  95.  
  96. //Box
  97. Box centerBox = Box.createVerticalBox();
  98. Box centerBox2 = Box.createVerticalBox();
  99. Box centerBox3 = Box.createVerticalBox();
  100.  
  101. //Adding everything to centerPanel
  102. centerPanel.add(centerBox, 1, 0);
  103. centerPanel.add(centerBox2);
  104. centerPanel.add(centerBox3);
  105.  
  106. mainEntreeBox = new JRadioButton[5];
  107. drinkBox = new JRadioButton[10];
  108. sideBox = new JCheckBox[6];
  109.  
  110. //Setting up choices
  111. String[] drink = {"Root beer", "Sprite", "Coffee", "Hot Chocolate", "Whie Chocolate", "Jarritos", "Dr. Pepper", "Shake", "Tea", "Coke"};
  112. String[] side = {"Salad", "Soup", "French Fries", "Potato salad", "Apple sauce", "Fruit"};
  113. String[] mainEntree = {"Hamburger", "Cheeseburger", "Hotdog", "Ham sandwhich", "Turkey sandwhich"};
  114.  
  115. JLabel chooseEntree = new JLabel("Choose your main entree: ");
  116. JLabel chooseSide = new JLabel("Choose your sides:");
  117. JLabel chooseDrink = new JLabel("Choose your drink:");
  118.  
  119. //Main Entree
  120. centerBox.add(chooseEntree);
  121. for(int i = 0; i < 5; i++)
  122. {
  123. mainEntreeBox[i] = new JRadioButton(mainEntree[i]);
  124. entree.add(mainEntreeBox[i]);
  125. centerBox.add(mainEntreeBox[i]);
  126. }
  127.  
  128. //Sides
  129. centerBox2.add(chooseSide);
  130. for(int i = 0; i < 6; i++)
  131. {
  132. sideBox[i] = new JCheckBox(side[i]);
  133. centerBox2.add(sideBox[i]);
  134. }
  135.  
  136. //Drink
  137. centerBox3.add(chooseDrink);
  138. for(int i = 0; i < 10; i++)
  139. {
  140. drinkBox[i]= new JRadioButton(drink[i]);
  141. centerBox3.add(drinkBox[i]);
  142. drinkGroup.add(drinkBox[i]);
  143. }
  144.  
  145. //Declaring and adding ActionListener
  146. SubmitAction action = new SubmitAction();
  147. submit.addActionListener(action);
  148. }
  149.  
  150. private class SubmitAction implements ActionListener
  151. {
  152. public void actionPerformed(ActionEvent event)
  153. {
  154. String fileName = new String();
  155. file = JOptionPane.showInputDialog(null, "Enter the file name to store your order to:");
  156.  
  157. try
  158. {
  159. //writeTo = new File(file + ".txt");
  160. if(fileChosen == false)
  161. {
  162. writer = new BufferedWriter(new FileWriter(file + ".txt"));
  163. fileName = file;
  164. fileChosen = true;
  165. }
  166.  
  167. else
  168. {
  169. if(!file.equals(fileName))
  170. {
  171. writer = new BufferedWriter(new FileWriter(file + ".txt"));
  172. fileName = file;
  173. }
  174.  
  175. }
  176.  
  177. int i, a, b;
  178.  
  179. writer.write("Entree chosen:");
  180. for(i = 0; i < 5; i++)
  181. {
  182. //Write entree chosen to file
  183. if(mainEntreeBox[i].isSelected())
  184. {
  185. writer.write(mainEntreeBox[i].getText());
  186. writer.newLine();
  187. }
  188. }
  189.  
  190. //write drink chosen to file
  191. writer.write("Drink chosen:");
  192. for(a = 0; a < 10; a++)
  193. {
  194. if(drinkBox[a].isSelected())
  195. {
  196. writer.write(drinkBox[a].getText());
  197. writer.newLine();
  198. }
  199. }
  200.  
  201. //write sides chosen to file
  202. writer.write("Side(s) chosen:");
  203. writer.newLine();
  204. for(b = 0; b < 6; b++)
  205. {
  206. if(sideBox[b].isSelected())
  207. {
  208. writer.write(sideBox[b].getText());
  209. writer.newLine();
  210. }
  211. }
  212. writer.close();
  213. }
  214.  
  215. catch(IOException e)
  216. {
  217. JOptionPane.showMessageDialog(null, "Error: unable to process order.\nPlease try again");
  218. }
  219. }
  220. }
  221. //datafields
  222. JRadioButton[] mainEntreeBox;
  223. JRadioButton[] drinkBox;
  224. JCheckBox[] sideBox;
  225.  
  226. String file;
  227. File writeTo;
  228. BufferedWriter writer;
  229.  
  230. boolean fileChosen = false;
  231. }
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Dec 2nd, 2008
0

Re: File I/0

If you want to use same file without loosing original content then you need to use append() method inherited from Writer class
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004
Dec 2nd, 2008
0

Re: File I/0

Do I just do it like this? writer.append(fileName + ".txt");
I tried it like that in my if else statement for if the file name is the same as before, and that didnt work.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Dec 2nd, 2008
0

Re: File I/0

Ah bugger, seems like I'm little rusty on basic file I/O. Forget append.
In your case you need to only use different FileWritter constructor FileWriter(File file, boolean append)
Java Syntax (Toggle Plain Text)
  1. writer = new BufferedWriter(new FileWriter(file + ".txt", true));
Last edited by peter_budo; Dec 2nd, 2008 at 8:52 pm. Reason: Adding code
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004
Dec 2nd, 2008
0

Re: File I/0

thanks! That worked! I really appreciate your help, I was really having trouble with File I/O but I think I get how to do it now!
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: Animation of Data Structures
Next Thread in Java Forum Timeline: Koriean text in CSV file...





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


Follow us on Twitter


© 2011 DaniWeb® LLC