Putting text fields in an array
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:
Code :
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();*/
}
Re: Putting text fields in an array
OK I've been out of java for a while so I mightn't be right and I can't actually run your code right now but I did skim through your code and a few things.
First off I kind of forget how actionlisteners work but I think you would only want to run the calculation when your calculation button is clicked so you don't need one for each text field. Secondly in this part
Code :
num1field = blood[0];
num2field = blood[1];
num3field = blood[2];
num4field = blood[3];
num5field = blood[4];
You are trying to set the fields to the values in the array I assume you want to do the opposite.
I think you need to rethink the structure.
The way I see the problem is a user enters 5 values once the calc button is clicked this calls a method that gets the 5 values and puts them in an array. At this stage you will want to convert them to integers and have a Try Catch for when a user puts in text, you'll also want to ensure a user can't put in negative numbers. Once this is done you'll want to calculate the average, I would recommend doing this in a seperate function not on the fly just as good practice, hell maybe even make a class that takes in an array of ints and has functions that return the sum, product, average but that's far from necessary.
Now theres some other things but just go back change your code and see if you can do it yourself bearing that in mind.
Re: Putting text fields in an array
Thanks for helping, but I already handed the program in today. I just put the values into new variables as integers and then added them up and divided by 5 to get the average. Even if I did it wrong it doesn't matter anymore. How am I expected to know how to put text field data in an array if I was never taught how to do it? idk what he was thinking. Thanks for trying though.