Hello, I'm a bit new to Java and I'm wondering how to do arrays with JTextField, so how would i go about starting this program here..

Build a GUI to accept integers (when the ENTER key is depressed) into an array of 15 int; have a button called AVERAGE; when AVERAGE is selected, there is a display of the count and the current average; have a button called CLEAR; when CLEAR is selected, count is set to 0 and so are all the array elements and then there is another display: count is 0 and average is 0.0.

Recommended Answers

All 4 Replies

The first step is to create a GUI which has what you need.
Then you need to add some listeners to those buttons and keypresses you want the program to respond to.
Then you need to add the programming to those listeners so that the program will do what you intend it to do. In this case add the integers into an array, calculate and display the average, and clear the array.

If you get any errors, feel free to post your code (remember to wrap it in [code] code goes here [/code] tags) and the text of the errors.

Thanks David,

I'm too just learning the ropes with Java. I appreciate your introduction to the subject.

package argui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ArrayGUIPanel extends JPanel
{
   private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageLabel;
 
   private JTextField nums;
   private JButton average, clear;
   private int count;
   private double avg;
   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public ArrayGUIPanel()
   {
 
       count = 0;
       avg=0.0;
 
      inputLabel = new JLabel ("Enter integers: " );
      resultLabel = new JLabel ("---");
      countLabel = new JLabel ("Count: " + count);
      averageLabel = new JLabel ("Average: " + avg);
 
 
 
      average = new JButton ("AVERAGE");
      clear = new JButton ("CLEAR");
      AvgListener listener = new AvgListener();
      average.addActionListener (listener);
      clear.addActionListener(listener);
      add (inputLabel);
      add (average);
      add (clear);
      add (resultLabel);
      add (countLabel);
      add (averageLabel);
      setPreferredSize (new Dimension(300, 75));
      setBackground (Color.white);
 
   }
 
   //*****************************************************************
   //  Represents an action listener for the average input field.
   //*****************************************************************
   private class AvgListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Performs the conversion when the enter key is pressed in
      //  the text field.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         int numsTemp, averageTemp;
         String text = nums.getText();

okay am i going in the right direction here? how does the array come in?

The first thing to do is NOT to build that GUI but to figure out what you're actually trying to do.

Without knowing that you can never expect to create a user interface which actually makes sense, let alone the code behind it to work with the data entered and produce the data you want to produce.

Your array is meant to store numbers entered into the user interface for later calculation of their average.
A rather limited implementation, but I guess you haven't come to the point of learning about more appropriate data structures yet so it'll have to do for now.
Just make sure you have some checks on the number of inputs in there so you don't try to insert more than 15 elements into the array :)

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.