1

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>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sarina Foreman
  • 11
  • 1
  • 1
  • 5
  • Btw, `double maximum = studentScores[1];` - note that arrays are zero-based so the first element will be `studentScores[0]` etc. – Thomas Oct 30 '18 at 16:22
  • I'll not comment on the multiple design issues as you are clearly still learning and that would be too much for this question (you could ask on the codereview site for that) but have a look here: `outfile.printf("\n" , studentNames[i]);` - you are printing the start tag (which you see in your output) but don't use the supplied name. You'll want to add `%s` to your format string, i.e. `"\n%s"`, to have the name added to the string that's printed. – Thomas Oct 30 '18 at 16:27
  • Why don't you use an API like JAXB to [generate XML](https://stackoverflow.com/questions/35785/xml-serialization-in-java)? And to compute the statistics you could use `IntStream.summaryStatistics()` which will return `IntSummaryStatistics`. Actually you could get most of the work out of the box. – LuCio Oct 30 '18 at 19:29

2 Answers2

0
if(studentScores[i] < average)
{
 outfile.printf("\n<belowAvg>" , studentNames[i]);
 outfile.println("</belowAvg>");
}

In the line where you mean to print the value from the studentNames array, you're not actually using the value. You need to add %s after your <belowAvg> tag.

Gary E.
  • 1
  • 1
0

you should use index 0 instead of 1 when initializing maximum and minimum. The reason for not showing the names is you didn't use %s specifier in printf.

double maximum = studentScores[0];
double minimum = studentScores[0];

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>%s" , studentNames[i]);
         outfile.println("</belowAvg>");
     }
 }

outfile.println("\n</students>");
outfile.close();
Shudipta Sharma
  • 5,178
  • 3
  • 19
  • 33