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();
						
}

I have noticed that you don't close anything and that your file doesn't have extension:

String filename = "grade" + myFormat.format(today);

Shouldn't be like:

String filename = "grade" + myFormat.format(today) + ".txt";

I don't know if the above will solve your problem but try to add the .txt and to close:
FileOutputStream, DataOutputStream
Check the API for more details since I don't remember 100% that both of them have a close() method. And if they do then close them in the opposite order you have opened them:

FileOutputStream fileStream = null;
  DataOutputStream output;
  
  fileStream = new FileOutputStream(filename, true);    
  output = new DataOutputStream(fileStream);


  output.close(); 
  fileStream.close();

Again you might want to check which one of them has a close() method.

Thanks. I didn't add everything because it was an example I just made to show what my problem is. On my original version I have the output location and file done. Just having a problem with accumulating totals so that if the user clicks add and if like 8 data entries are added then they select enter, in the data file it will show all 8 entries when they open it up.

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.