RThomP RThomP - 7 months ago 14
Java Question

Java calculation problems

Long question here, just a disclaimer.

I'm trying to code an average score "calculator" with Eclipse. It's a university assignment and the "scenario" is a fictional winter olympics event, focusing on snowboarding. Each athlete has 2 runs, with 7 judges each giving a score out of 10.
I'm using Eclipse with WindowBuilder to build the GUI. Scores are entered into 7 individual textfields named txtRun1_0; txtRun1_1; txtRun1_2; etc. through to txtRun1_6. As seen in the code below, I am storing the strings in an array, and then parsing each string to a double in another equally sized array for doubles. Then I continue with the calculation.

The confusing part is that the code below is simply adapted to work with a GUI; I originally wrote it to work with the console, and it works perfectly. I will include the original code as well. As you will see, the main calculation parts of the code are nearly identical, apart from a couple of very slight changes in variable names.

EDIT: The problem with the program is this: If I enter 1, 2, 3, 4, 5, 6, and 7 as the scores, it returns the average of the scores as 0.0, and the highest AND lowest of the scores is 7.0.

The program has to adhere to the following criteria:


  • Highest and lowest scores are truncated from the calculation

  • Arrays are a must



I've double checked and triple checked the names for all my labels and variables, but I haven't found anything wrong with it.

Here is my code for the GUI program (for run 1):

//Start Variables Declaration
double [] daRun1 = new double[7]; //Run1 Array
double [] daRun2 = new double[7]; //Run2 Array
String [] saRun1 = new String[7]; //Run1 String Array
String [] saRun2 = new String[7]; //Run2 String Array
int di1; //daRun1 index integer
int di2; //daRun2 index integer
int si1; //saRun1 index integer
int si2; //saRun2 index integer

//End Variables Declaration

///////////////
//Run 1 Start//
////////////////////////////////////////////////////////
//Get values from JTextFields (txtRun1_0 to txtRun1_6)//
//and enter them in to their respective arrays//////////
//Run 1 Scores//////////////////////////////////
///////////////
saRun1[0] = txtRun1_0.getText();
saRun1[1] = txtRun1_1.getText();
saRun1[2] = txtRun1_2.getText();
saRun1[3] = txtRun1_3.getText();
saRun1[4] = txtRun1_4.getText();
saRun1[5] = txtRun1_5.getText();
saRun1[6] = txtRun1_6.getText();

//Parses string values of saRun1 to integers
//and enters them into daRun1 (array of doubles)
//to make calculation of average scores possible
for (di1=0; di1<daRun1.length; di1++)
{
for (si1=0; si1<saRun1.length; si1++)
{
daRun1[di1] = Double.parseDouble(saRun1[si1]);
}
}

//Finds the maximum and minimum scores
//for Run 1, ready for truncating them during
//average score calculation
double min1 = daRun1[0];
double max1 = daRun1[0];

for (di1=0; di1<daRun1.length; di1++)
{
if (daRun1[di1] > max1)
{max1 = daRun1[di1];}

if (daRun1[di1] < min1)
{min1 = daRun1[di1];}
}

//These println statements are there to help me debug the code; the result of the calculation is allocated to a label further down in the code.
System.out.println("The maximum score for Run 1 is: " + max1 + "\nThe minimum score for Run 1 is: " + min1);

//Calculates the average score
//for Run 1 (not including the maximum and minimum scores)
double sum1 = 0.0;
double avg1 = 0.0;

for (di1=0 ; di1<daRun1.length ; di1++)
{
if(daRun1[di1] == max1 || daRun1[di1] == min1)
continue;
sum1 += daRun1[di1];
}
avg1 = sum1 / (daRun1.length-2);


//Output to lblAvgRun1
lblAvgRun1.setText(Double.toString(avg1));
System.out.println(avg1);


And here is my code for the console version:

double [] daRun1 = new double[7]; //Run1 Array
double [] daRun2 = new double[7]; //Run2 Array
int i1; //daRun1 index integer
int i2; //daRun2 index integer


//Initializes console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


////////////////
//Run 1 Start///
////////////////

System.out.println("Enter the 7 scores for run 1:");

//Run 1 Data Entry below
for (i1=0; i1<daRun1.length; i1++)
{
daRun1[i1] = Double.parseDouble(br.readLine());
}

//Printing Array Elements - Run 1
for (i1=0; i1<daRun1.length; i1++)
{
System.out.println(i1+1 + " " + daRun1[i1]);
}

//Find maximum and minimum scores - Run 1
double min1 = daRun1[0];
double max1 = daRun1[0];

for (i1=0; i1<daRun1.length; i1++)
{
if (daRun1[i1] > max1)
{max1 = daRun1[i1];}

if (daRun1[i1] < min1)
{min1 = daRun1[i1];}
}
System.out.println("The maximum score for Run 1 is: " + max1 + "\nThe minimum score for Run 1 is: " + min1);

//Calculate the average score - Run 1
double sum1 = 0.0;
double avg1 = 0.0;

for (i1=0 ; i1<daRun1.length ; i1++)
{
if(daRun1[i1] == max1 || daRun1[i1] == min1)
continue;
sum1 += daRun1[i1];
}
avg1 = sum1 / (daRun1.length-2);

System.out.println("The average score for Run 1 is: " + avg1);
//End Run 1//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Answer

Try changing:

for (si1=0; si1<saRun1.length; si1++)
{
    daRun1[di1] = Double.parseDouble(saRun1[si1]);
}

To:

for (si1=0; si1<saRun1.length; si1++)
{
    daRun1[si1] = Double.parseDouble(saRun1[si1]);
}

You are just overwriting the same array element every time you loop through...

Also I'm not sure why you have the outer loop there because it is not a multi-dimensional array. The inner loop should be enough...