Currently having a problem with adding data so that if someone selects something like an add button, it keeps adding the data, like a person's name, age and grade. Then when they select Enter, all the data they added will display in a data file. The problem I'm having is if I select enter, the previous entry that was added is the only one that displays in the datafile instead of all the data I've entered. How can I create a class to handle that. So when the person selects add it keeps adding the data, then when they finally select enter all the data they've added will display in the datafile created. This is just a sample code I created so most of the info isn't accurate. Just wanted you guys to see what I'm trying to do. Thanks again guys.
int age;
double grade;
FormattedTextField name = new JFormattedTextField();
FormattedTextField gradeField = new JFormattedTextField();
FormattedTextField ageField = new JFormattedTextField();
JTextField showAge = new JTextField(8);
JTextField showGrade = new JTextField(8);
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Add")
calculate();
else if(arg == "Enter")
write();
}
private void calculate()
{
age = Int.parseInt(ageField.getText());
grade = Double.parseDouble(gradeField.getText());
showAge.setText("Age: " + age);
showGrade.setText("Grade: " + grade);
}
public void write()
{
Date today = new Date();
SimpleDateFormat myFormat = new SimpleDateFormat("MMddyyyy");
String filename = "grade" + myFormat.format(today);
try
{
FileOutputStream fileStream = null;
DataOutputStream output;
fileStream = new FileOutputStream(filename, true);
output = new DataOutputStream(fileStream);
try
{
output.writeUTF(name.getText());
output.writeInt(age);
output.writeDouble(grade);
JOptionPane.showMessageDialog(null, "The information has been added.", "Submitted", JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException c)
{
enterArea();
}
private void enterArea()
{
write();
}