File I/0
Please support our Java advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 107
Reputation:
Solved Threads: 1
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;
} 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
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)
In your case you need to only use different FileWritter constructor FileWriter(File file, boolean append)
Java Syntax (Toggle Plain Text)
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
Other Threads in the Java Forum
- connect to text file database (Visual Basic 4 / 5 / 6)
- Importing SQL Script File - Urgent !! (Database Design)
- Syntax for deleting specified file ( in C++) (C++)
- enabling file sharing (Windows NT / 2000 / XP / 2003)
- data file help (C)
- 81TB File Server (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: Animation of Data Structures
- Next Thread: Koriean text in CSV file...
•
•
•
•
Views: 333 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode