I'm writing a program which collects company blood drive amounts into a Java applet and my teacher wants me to find the average of the amount of blood by putting the answers that the user types into an array. I'm unsure of how to do this, obviously the way i'm doing it is wrong because they are incompatible types and i'm unsure of how to put the answers into an array. Also, I want to have the user hit a reset button so they don't have to run the program multiple times, but each time the enter key is pressed it clears the boxes and if I click on the buttons in the applet they do not work. Please please help. Thank you.
The code is below:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class blooddrive extends JApplet implements ActionListener { JLabel instruct = new JLabel(" Enter pints of blood for each department"); Font insfont = new Font("Verdana", Font.BOLD, 20); JLabel num1lab = new JLabel(" Department 1"); JTextField num1field = new JTextField(7); JLabel num2lab = new JLabel("Department 2"); JTextField num2field = new JTextField(7); JLabel num3lab = new JLabel("Department 3"); JTextField num3field = new JTextField(7); JLabel num4lab = new JLabel("Department 4"); JTextField num4field = new JTextField(7); JLabel num5lab = new JLabel("Department 5"); JTextField num5field = new JTextField(7); JLabel answerlab = new JLabel("Average"); JTextField answerfield = new JTextField(9); JButton calc = new JButton("Find Average"); JButton nbut = new JButton("Reset"); FlowLayout flow = new FlowLayout(); Container c; public void init() { c = getContentPane(); c.setLayout(flow); c.setBackground(Color.black); instruct.setFont(insfont); instruct.setForeground(Color.blue); c.add(instruct); num1lab.setForeground(Color.blue); c.add(num1lab); num1field.setForeground(Color.blue); c.add(num1field); num2lab.setForeground(Color.blue); c.add(num2lab); num2field.setForeground(Color.blue); c.add(num2field); num3lab.setForeground(Color.blue); c.add(num3lab); num3field.setForeground(Color.blue); c.add(num3field); num4lab.setForeground(Color.blue); c.add(num4lab); num4field.setForeground(Color.blue); c.add(num4field); num5lab.setForeground(Color.blue); c.add(num5lab); num5field.setForeground(Color.blue); c.add(num5field); answerlab.setForeground(Color.blue); c.add(answerlab); answerfield.setForeground(Color.blue); answerfield.setEnabled(false); c.add(answerfield); calc.setForeground(Color.blue); c.add(calc); nbut.setForeground(Color.blue); c.add(nbut); num1field.requestFocus(); num2field.addActionListener(this); num3field.addActionListener(this); num4field.addActionListener(this); num5field.addActionListener(this); nbut.addActionListener(this); } public void actionPerformed(ActionEvent e) { int[] blood = new int[5]; int sum = 0, i; double avg; num1field = blood[0]; num2field = blood[1]; num3field = blood[2]; num4field = blood[3]; num5field = blood[4]; for (i = 0; i < blood.length; i++) sum += blood[i]; avg = (double)sum / blood.length; } /*num1field.setEnabled(false); num2field.setEnabled(false); num3field.setEnabled(false); num4field.setEnabled(false); num5field.setEnabled(false); calc.setEnabled(false); num1field.setText(""); num2field.setText(""); num3field.setText(""); num4field.setText(""); num5field.setText(""); num1field.setEnabled(true); num2field.setEnabled(true); num3field.setEnabled(true); num4field.setEnabled(true); num5field.setEnabled(true); calc.setEnabled(true); num1field.requestFocus();*/ }