Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Putting text fields in an array

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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:

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


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default 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
     
    			  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.
    Last edited by Faz; March 10th, 2010 at 08:49 AM.

  3. The Following User Says Thank You to Faz For This Useful Post:

    Movies32 (March 10th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 02:37 PM
  2. Static fields and inheritance
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 22nd, 2010, 03:02 AM
  3. Coloured text
    By silverspoon34 in forum Java Theory & Questions
    Replies: 8
    Last Post: November 26th, 2009, 06:21 PM
  4. inputting of text
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM
  5. How to format text in java?
    By fourseven in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2009, 09:42 PM

Tags for this Thread