RSS Forums RSS

File I/0

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 I/0

  #1  
Dec 2nd, 2008
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.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
 * Program that allows user to select a meal.
 * Includes sides, drink, and main entree
 * Writes users order to user specified file.
 * @author Kimberlie
 * @version 12/4/08
 */
public class MealBuilder {

    /*
     * Main
     * Sets up frame
     * @param args not used
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(
           new Runnable()
           {
                public void run()
                {
                   MealBuilderFrame frame = new MealBuilderFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
                }
           });
    }

}

/*
 * MealBuilderFrame class
 * extends JFrame
 * Sets up frame, sets size and title
 * Declares and adds panel
 */
class MealBuilderFrame extends JFrame
{
    /*
     * MealBuilderFrame constructor
     * @param none
     */
    public MealBuilderFrame()
    {
      this.setTitle("Meal Builder");
      this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      MealBuilderPanel panel = new MealBuilderPanel();
      this.add(panel);
    }

    final static int DEFAULT_WIDTH = 550;
    final static int DEFAULT_HEIGHT = 400;
}

/*
 * MealBuilderPanel class
 * extends JPanel
 * Sets up panel with components
 */
class MealBuilderPanel extends JPanel
{
    /*
     * MealBuilderPanel constructor
     * @param none
     */
    public MealBuilderPanel()
    {
        this.setLayout(new BorderLayout());

        //ButtonGroup for radio buttons
        ButtonGroup entree = new ButtonGroup();
        ButtonGroup drinkGroup = new ButtonGroup();

        //North
        this.add(new JLabel("Server: Kimberlie Davis"), BorderLayout.NORTH);

        //West
        this.add(new JLabel("           "), BorderLayout.WEST);

        //Center
        JPanel centerPanel = new JPanel(new GridLayout(1,4));
        this.add(centerPanel, BorderLayout.CENTER);

        //south
        JPanel southPanel = new JPanel();
        this.add(southPanel, BorderLayout.SOUTH);
        JButton submit = new JButton("Order");
        southPanel.add(submit);

        //Box
        Box centerBox = Box.createVerticalBox();
        Box centerBox2 = Box.createVerticalBox();
        Box centerBox3 = Box.createVerticalBox();

        //Adding everything to centerPanel
        centerPanel.add(centerBox, 1, 0);
        centerPanel.add(centerBox2);
        centerPanel.add(centerBox3);

        mainEntreeBox = new JRadioButton[5];
        drinkBox = new JRadioButton[10];
        sideBox = new JCheckBox[6];

        //Setting up choices
        String[] drink = {"Root beer", "Sprite", "Coffee", "Hot Chocolate", "Whie Chocolate", "Jarritos", "Dr. Pepper", "Shake", "Tea", "Coke"};
        String[] side = {"Salad", "Soup", "French Fries", "Potato salad", "Apple sauce", "Fruit"};
        String[] mainEntree = {"Hamburger", "Cheeseburger", "Hotdog", "Ham sandwhich", "Turkey sandwhich"};

        JLabel chooseEntree = new JLabel("Choose your main entree:     ");
        JLabel chooseSide = new JLabel("Choose your sides:");
        JLabel chooseDrink = new JLabel("Choose your drink:");

        //Main Entree
        centerBox.add(chooseEntree);
        for(int i = 0; i < 5; i++)
        {
            mainEntreeBox[i] = new JRadioButton(mainEntree[i]);
            entree.add(mainEntreeBox[i]);
            centerBox.add(mainEntreeBox[i]);
        }

        //Sides
        centerBox2.add(chooseSide);
        for(int i = 0; i < 6; i++)
        {
            sideBox[i] = new JCheckBox(side[i]);
            centerBox2.add(sideBox[i]);
        }

        //Drink
        centerBox3.add(chooseDrink);
        for(int i = 0; i < 10; i++)
        {
            drinkBox[i]= new JRadioButton(drink[i]);
            centerBox3.add(drinkBox[i]);
            drinkGroup.add(drinkBox[i]);
        }

        //Declaring and adding ActionListener
        SubmitAction action = new SubmitAction();
        submit.addActionListener(action);
    }

    private class SubmitAction implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
          String fileName = new String();
          file = JOptionPane.showInputDialog(null, "Enter the file name to store your order to:");

          try
          {
             //writeTo = new File(file + ".txt");
             if(fileChosen == false)
             {
                writer = new BufferedWriter(new FileWriter(file + ".txt"));
                fileName = file;
                fileChosen = true;
             }

             else
             {
                 if(!file.equals(fileName))
                 {
                    writer = new BufferedWriter(new FileWriter(file + ".txt"));
                    fileName = file;
                 }

             }

             int i, a, b;

             writer.write("Entree chosen:");
             for(i = 0; i < 5; i++)
             {
              //Write entree chosen to file
                if(mainEntreeBox[i].isSelected())
                {
                    writer.write(mainEntreeBox[i].getText());
                    writer.newLine();
                }
             }

              //write drink chosen to file
              writer.write("Drink chosen:");
              for(a = 0; a < 10; a++)
              {
                  if(drinkBox[a].isSelected())
                  {
                    writer.write(drinkBox[a].getText());
                    writer.newLine();
                  }
              }

              //write sides chosen to file
              writer.write("Side(s) chosen:");
              writer.newLine();
              for(b = 0; b < 6; b++)
              {
                  if(sideBox[b].isSelected())
                  {
                    writer.write(sideBox[b].getText());
                    writer.newLine();
                  }
              }
              writer.close();
            }

            catch(IOException e)
            {
                JOptionPane.showMessageDialog(null, "Error: unable to process order.\nPlease try again");
            }
          }
    }
    //datafields
    JRadioButton[] mainEntreeBox;
    JRadioButton[] drinkBox;
    JCheckBox[] sideBox;

    String file;
    File  writeTo;
    BufferedWriter writer;

    boolean fileChosen = false;
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 3,460
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: File I/0

  #2  
Dec 2nd, 2008
If you want to use same file without loosing original content then you need to use append() method inherited from Writer class
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
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 I/0

  #3  
Dec 2nd, 2008
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.
Reply With Quote  
Posts: 3,460
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: File I/0

  #4  
Dec 2nd, 2008
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)
  1. writer = new BufferedWriter(new FileWriter(file + ".txt", true));
Last edited by peter_budo : Dec 2nd, 2008 at 7:52 pm. Reason: Adding code
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
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 I/0

  #5  
Dec 2nd, 2008
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!
Reply With Quote  
Reply

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



Similar Threads
Other Threads in the Java Forum
Views: 333 | Replies: 4 | 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:30 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