I have a program that asks for a student name and score. The name and score are separated by a "|". I have split those and placed them each into an array so that I can display statistics based on the scores.
I'm running into a problem when I try to display the names of students who have scored below the average. Whenever I run the program nothing prints out for the <belowAvg>
tag. I don't think that the names are being saved into the studentNames[i]
file.
I would appreciate if someone can point me in the right direction.
// Get the data from the text area and dump it in the file in XML format
String text = textArea.getText();
// Print the text to the file - for testing purposes only
outfile.println(text);
Double[] studentScores = new Double[10];
String [] studentNames = new String[10];
double sumScores = 0;
String []lines = text.split("\n");
int i;
outfile.println("<students>");
for(i=0;i<lines.length;i++)
{
outfile.println("<student>");
String[]tokens =lines[i].split("\\|");
outfile.println("<name>" + tokens[0] + "</name>");
studentNames[i] = tokens[0];
outfile.println("<score>" + tokens[1] + "</score>");
Double score = Double.parseDouble(tokens[1]);
studentScores[i] = score;
outfile.println("</student>");
}
double arraySize = i;
double average = 0;
double maximum = studentScores[1];
double minimum = studentScores[1];
for(i=0;i<arraySize;i++)
{
sumScores = sumScores + studentScores[i];
if(studentScores[i] > maximum)
{
maximum = studentScores[i];
}
if(studentScores[i] < minimum)
{
minimum = studentScores[i];
}
}
average = sumScores / arraySize;
outfile.printf("\nThe sum is: %.1f" , sumScores);
outfile.printf("\n<average> %.1f", average);
outfile.println("</average>");
outfile.printf("\n<maximum> %.1f" , maximum);
outfile.println("</maximum>");
outfile.printf("\n<minimum> %.1f", minimum);
outfile.println("</minimum");
for(i=0;i<arraySize;i++)
{
if(studentScores[i] < average)
{
outfile.printf("\n<belowAvg>" , studentNames[i]);
outfile.println("</belowAvg>");
}
}
outfile.println("\n</students>");
outfile.close();
This is what the XML file is printing.
jill|87
phil|23
michael|99
leny|67
<students>
<student>
<name>jill</name>
<score>87</score>
</student>
<student>
<name>phil</name>
<score>23</score>
</student>
<student>
<name>michael</name>
<score>99</score>
</student>
<student>
<name>leny</name>
<score>67</score>
</student>
The sum is: 276.0
<average> 69.0</average>
<maximum> 99.0</maximum>
<minimum> 23.0</minimum
<belowAvg></belowAvg>
<belowAvg></belowAvg>
</students>