I would like some help to index through an empty array and fill it with 10 entries one at a time. The objective of the task is to create a GUI with two buttons, one (Store) to save the value into array position and second (Quit) to run statistics calculations on stored inputs. The calculations code works because I tested it with hard coded data...in other words this code line produces the expected results for mean, sum, above and below average.

static int[] occupants = {3,0,4,1,2,3,2,3,0,2};

What I really want to do is every time I enter array position and value, it is stored and calculations performed. Then repeat the process for each of the values above (or desired entries except that there is a maximum 10 positions. Work in progress program is below.

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIStats {
static final int BUILDING_SIZE = 10;              // No. of appartment constant declared
static int[] occupants = new int[BUILDING_SIZE];  // Allocate appartment array variable
//static int[] occupants = {3,0,4,1,2,3,2,3,0,2};   // add this (manual array input)
static final int MAX_OCCUPANTS = 20;              // Max occupants constant defined
static int sum = 0;                   // Declare total results holder
static double mean = 0;               // Declare average results holder
static int above = 0;                 // Declare above average results holder
static int below = 0;                 // Declare below average result holder
static int index = 0;
static int total = 0;
static JFrame outFrame;           // Declare a frame
static Container outPane;         // Declare content pane of frame
static JButton store;             // Declare a store button
static JButton quit;              // Declare a quit button
static int ApartmentInput;        // Stores apartment entry in I/O text field
static int OccupantsInput;        // Stores occupants entry in I/O text field
static NumericHandler operation;  // Declare the listener  
// Declare Labels for display statements and results in GUI frame
static JLabel totalRegistry;
static JLabel sumRegistry;
static JLabel meanRegistry;
static JLabel aboveRegistry;
static JLabel belowRegistry;
static JLabel testresult;
static JLabel totresult;
static JLabel avgresult;
static JLabel abvresult;
static JLabel belresult;
static JLabel aptinput;
static JLabel occinput;
static JLabel storeinput;
static JLabel calcresult;
// Declare input I/O fields for GUI frame
static JTextField aptL;
static JTextField occR;
// ************************ Start the listening  process here ****************************
static class NumericHandler implements ActionListener {
// Listener for the operations buttons
public void actionPerformed(ActionEvent event) {
// Converts string in inputField to integer value
String whichButton;                              // This variable hold's the button's name
index = ApartmentInput;
//ApartmentInput = Integer.parseInt(aptL.getText());
//OccupantsInput = Integer.parseInt(occR.getText());
ApartmentInput = Integer.valueOf(aptL.getText()).intValue();
OccupantsInput = Integer.valueOf(occR.getText()).intValue();
whichButton = event.getActionCommand();              // Gets the button's name
if (whichButton.equals("Store")) {                   // Proceeds on the basis of store button
for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
occupants[index] = OccupantsInput;                   // Reads in the occupants inputs
}
total = total + OccupantsInput;
totalRegistry.setText("" + total);
aptL.setText("0");                                   // Keeps input fields cleared
occR.setText("0");                                   // Keeps input fields cleared
System.out.println("Stored total is: " + total);     // *****Just checking reaction
System.out.println("Occupants in APT " + ApartmentInput + " is: " + occupants[OccupantsInput]);
}
else                                                 // Otherwise the quit button
{
//********************** Begin the statistical permutations here *************************
// The following calculated results produces two values for the above and below averages
//  (1) Showing the assignment requirements and (2) revealing what the above and below
//  actual array values are and printing out on screen - outside of the GUI. All other
//  results are also printed out onscreen. The assignment requirements are
//  only displayed within the generated GUI.
for (int index = 0; index < occupants.length; index++) {
// Calculate sum
sum += occupants[index];
}
sumRegistry.setText("" + sum);
System.out.println("The sum is: " + sum);
// Calculate average
mean = sum / occupants.length;
String output = "The array is: ";
for (int index = 0; index < occupants.length; index++) {
output += occupants[index] + " ";
}
output += "\nThe mean is: " + mean;
meanRegistry.setText("" + mean);
System.out.println(output);
// Calculate above mean
String out = "The numbers above average are: ";
int above = 0;
for (int index = 0; index < occupants.length; index++) {
if (occupants[index] > mean) {
out += occupants[index] + " ";
above++;
}
}
aboveRegistry.setText("" + above);
System.out.println(out);
System.out.println("Above average occupancy number:  " + above);
// Calculate below mean
String out1 = "The numbers below average are: ";
int below = 0;
for (int index = 0; index < occupants.length; index++) {
if (occupants[index] < mean) {
out1 += occupants[index] + " ";
below++;
}
}
belowRegistry.setText("" + below);
System.out.println(out1);
System.out.println("Below average occupancy number:  " + below);
}
}
}
//********************* End of the statistical permutations here *************************
// ***************************End of the listening process *******************************

Thanks for the help.

Could you add some comments in the code showing where you logic needs help.
For example: // **** HERE I NEED HELP GETTING ELEMENTS INTO ARRAY ******

want to do is every time I enter array position and value, it is stored and calculations performed

int index =       // add code here to get the array position as an int
<type??> value = //add code to get the value

theArray[index] = value; // store the value in the array
// Add code here to do the calculation

Could you add some comments in the code showing where you logic needs help.
For example: // **** HERE I NEED HELP GETTING ELEMENTS INTO ARRAY ******

int index =       // add code here to get the array position as an int
<type??> value = //add code to get the value

theArray[index] = value; // store the value in the array
// Add code here to do the calculation

Thanks for the reply Norm1. I thought these code lines are doing as you suggest but the thing is the array is being filled with my first entry. I need to fill in the first entry, do calculations and input the second, do calculations and so on...Where I read in occupants input I need to do it one at a time pleae. Thanks.>

for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
occupants[index] = OccupantsInput; // Reads in the occupants inputs
}
for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
  occupants[index] = OccupantsInput; // Reads in the occupants inputs
  // Do calculations here
}

If you can't use a loop, you will have to handle the array index incrementing and testing yourself

for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
  occupants[index] = OccupantsInput; // Reads in the occupants inputs
  // Do calculations here
}

If you can't use a loop, you will have to handle the array index incrementing and testing yourself

You will have to please pardon my limited knowledge but I am not sure what is meant by <type??> and second,because of the nature of sequence of actions I am thinking a loop is necessary to sequence through the array positions one by one, do the calculations each time to see the results until the array is filled with ten positions. I asked the question before and the teacher said the only clue he will offer is that the line assigning the value is my only issue and to fix that...This threw me because I don't see how that is filling all the spots in the array in one shot. I need to understand what I am doing wrong with both these lines.

#
for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
#
occupants[index] = OccupantsInput; // Reads in the occupants inputs
#
}

what is meant by <type??>

Since I didn't know the type of the variable I indicated that by <type?>
Data types are such as int, float, byte, String. You'd use it like this:
int anInt = 1; // data type is int
String aString = "THERE"; // data type is String

I need to understand what I am doing wrong with both these lines.

There is nothing "wrong" with those 2 lines. They require some extra lines of code to define the array: occupants[], the variable: index and the variable: occupantsInput. Given those three things the code should compile and execute ok.

The question remains: what is the value of occupantsInput that is being assigned to all the elements of the array?

Since I didn't know the type of the variable I indicated that by <type?>
Data types are such as int, float, byte, String. You'd use it like this:
int anInt = 1; // data type is int
String aString = "THERE"; // data type is String


There is nothing "wrong" with those 2 lines. They require some extra lines of code to define the array: occupants[], the variable: index and the variable: occupantsInput. Given those three things the code should compile and execute ok.

The question remains: what is the value of occupantsInput that is being assigned to all the elements of the array?

Hello again,

My variables are ApartmentInput representing apartment number (array position) and OccupantsInput representing occupants in each apartment (array position). Both these variables are TextField input places created within a GUI. The sequence of operation is to input the appropriate values in the text fields, hit a store button to register the values and then a second button to perform statistics on the input data.

The actions of the buttons work as they are suppose to BUT, once I input the first pair of data it fills the array...example: ApartmentInput = 0 (or array position 1) and OccupantsInput = 2 (2 people in apartment 1) the array is filled as follows:
occupants[index] = {2,0,0,0,0,0,0,0,0,0}...// Why is this happening and how do I correct.

I need it to be:
occupants[index] = {2} // Do statistics calculations
occupants[index] = {2,0} // Do statistics calculations
occupants[index] = {2,0,0} // Do statistics calculations...and so on until array is filled only by my entries.

The code lines in question are as follows:

for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
occupants[index] = OccupantsInput; // Reads in the occupants inputs
// Do calculations here
}

occupants[index] = {2,0,0,0,0,0,0,0,0,0}...// Why is this happening and how do I correct.

I need it to be:
occupants[index] = {2} // Do statistics calculations
occupants[index] = {2,0} // Do statistics calculations
occupants[index] = {2,0,0} // Do statistics calculations...and so on until array is filled only by my entries.

What you show is a two dimensional array. The element in the occupants array at index: index is an the array: {2,0,0,0,0,0,0,0,0,0}
Is that what you intend?

If occupants is a one dimensional array it would look like this:
occupants = {2,0,0,0,0,0,0,0,0,0}; // shows the whole array
occupants[0] = 2; // looks at the first element
occupants[1] = 0; // look at the second element

If you print out Arrays.toString(occupants) it will show you the contents of the array.

To get the following:

I need it to be:
occupants[index] = {2} // Do statistics calculations
occupants[index] = {2,0} // Do statistics calculations
occupants[index] = {2,0,0} // Do statistics calculations...and so on until array is filled

You need a 2 dim array:
occupants[][] = new int[MAX][];

then in your code:
occupants[0] = new int[1]; // define 1 dim array
occupants[0][0] = 2; // assign a value to one element
occupants[0] is {2}
occupants[1] = new int[2]; // define 2 dim array
occupants[1][0] = 2; // assign a value to one element
occupants[1] is {2, 0}
etc

What you show is a two dimensional array. The element in the occupants array at index: index is an the array: {2,0,0,0,0,0,0,0,0,0}
Is that what you intend?

If occupants is a one dimensional array it would look like this:
occupants = {2,0,0,0,0,0,0,0,0,0}; // shows the whole array
occupants[0] = 2; // looks at the first element
occupants[1] = 0; // look at the second element

If you print out Arrays.toString(occupants) it will show you the contents of the array.

To get the following:

You need a 2 dim array:
occupants[][] = new int[MAX][];

then in your code:
occupants[0] = new int[1]; // define 1 dim array
occupants[0][0] = 2; // assign a value to one element
occupants[0] is {2}
occupants[1] = new int[2]; // define 2 dim array
occupants[1][0] = 2; // assign a value to one element
occupants[1] is {2, 0}
etc

Thanks again Norm1 for the reply.

I am describing a one dimensional array. The fact that I have the two variables (ApartmentInput and OccupantsInput)is for use only for control of the array in the GUI. The array is of occupants in 10 apartments, where the apartment number is the array position. The user enters the array position (apartment number) along with the occupants in that apartment. I would like to evaluate the data each time an entry is made and therefore I would like my array increased by one each time I press the "Store" button. Instead the array is filled with the first input of the OccupantsInput value. If my first entry is 2 for OccupantsInput, then all array positions is filled with 2's. Here is how my program starts: (attached doc. file)

I would like my array increased by one each time I press the "Store" button.

Are you sure?
Is this what you want:
first:
occupants[] = new int[1];

then:
occupants[] = new int[2]; // increase size of array by one

then:
occupants[] = new int[3]; // increase size of array by one


etc

Unusual requirement. Instead of increasing the size each time you press the button, why not set and save indexes or counter to the array that indicate the range and location of useful contents to use in the calculations.

Are you sure?
Is this what you want:
first:
occupants[] = new int[1];

then:
occupants[] = new int[2]; // increase size of array by one

then:
occupants[] = new int[3]; // increase size of array by one


etc

Unusual requirement. Instead of increasing the size each time you press the button, why not set and save indexes or counter to the array that indicate the range and location of useful contents to use in the calculations.

BINGO I don't want to do the first part, indeed I really need to do what you are suggesting...This is why I am asking for the help. Precisely your question back to me. I thought by using the loop statement below does just what you are suggesting and that is to set and save indexes (or counter if you will) to the array but I do not know how. I know when I input into my text fields, the inputs are stored and used because I see the results using my inputs. But why the code for(index = 0; index < occupants.length; index++;) {

Why isn't index++ stepping through one by one??? I need to do what you are suggesting but as you can see the newbie in my communications, I have difficulties with the language for asking my question. I need to set and save my inputs each time and use to step through the array but do not know how.

Why isn't index++ stepping through one by one?

It will unless the value of index is changed inside of the loop.

by using the loop statement .... to set and save indexes (or counter if you will) to the array

This has been shown and discussed before. I can not understand your problem with using a loop to set the values of all the elements in an array one by one until they are all set.

for(int i=0; i < array.length; i++ {
   array[i] = value; // set value of this element
} // end for(i) thru the array

Now all the elements in the array are set to "value".

It will unless the value of index is changed inside of the loop.


This has been shown and discussed before. I can not understand your problem with using a loop to set the values of all the elements in an array one by one until they are all set.

for(int i=0; i < array.length; i++ {
   array[i] = value; // set value of this element
} // end for(i) thru the array

Now all the elements in the array are set to "value".

Got it, Thanks Norm1 for helping.

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.