Hello,
I had to make this program and it is all working except it is not writting to the file. Can you help me out?

Here is the code:

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)
        {
          file = JOptionPane.showInputDialog(null, "Enter the file name to store your order to:");

          try
          {
             //writeTo = new File(file + ".txt");
  
             writer = new BufferedWriter(new FileWriter(file + ".txt"));
       
             writer.write("hi");
             int i, a, b;
             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
              for(a = 0; a < 10; a++)
              {
                  if(drinkBox[a].isSelected())
                  {
                    writer.write(drinkBox[a].getText());
                    writer.newLine();
                  }
              }
              
              //write sides chosen to file
              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; //= new File("hello.txt");
    BufferedWriter writer;
}

Try closing the writer: //writer.close(); Why did have that line in comment?
Remove the comment

that worked! Thanks! I dont know why but before when I had that line I would get an error. But I didnt this time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.