Converting data to XML while Processing it

import java.io.*;
import java.text.*;
import java.util.*;

public class BattingAverage {

  public static void main(String[] args) {

    try {
      FileInputStream fin = new FileInputStream(args[0]);
      BufferedReader in
       = new BufferedReader(new InputStreamReader(fin));

      FileOutputStream fout
       = new FileOutputStream("battingaverages.xml");
      OutputStreamWriter out
       = new OutputStreamWriter(fout, "UTF-8");
      out.write("<?xml version=\"1.0\"?>\r\n");
      out.write("<players>\r\n");
      String playerStats;

      // for formatting batting averages
      DecimalFormat averages = (DecimalFormat)
        NumberFormat.getNumberInstance(Locale.US);
      averages.setMaximumFractionDigits(3);
      averages.setMinimumFractionDigits(3);
      averages.setMinimumIntegerDigits(0);

      while ((playerStats = in.readLine()) != null) {
        String[] stats = splitLine(playerStats);

        String formattedAverage;
        try {
          int atBats         = Integer.parseInt(stats[6]);
          int hits           = Integer.parseInt(stats[8]);
          int walks          = Integer.parseInt(stats[19]);
          int hitByPitch     = Integer.parseInt(stats[21]);
          int sacrificeFlies = Integer.parseInt(stats[16]);
          int sacrificeHits  = Integer.parseInt(stats[15]);

          int officialAtBats
           = atBats - walks - hitByPitch - sacrificeHits;
          if (officialAtBats <= 0) formattedAverage = "N/A";
          else {
            double average = hits / (double) officialAtBats;
            formattedAverage = averages.format(average);
          }
        }
        catch (Exception e) {
          // skip this player
          continue;
        }

        out.write("  <player>\r\n");
        out.write("    <first_name>" + stats[1] + "</first_name>\r\n");
        out.write("    <surname>" + stats[0] + "</surname>\r\n");
        out.write("    <batting_average>" + formattedAverage
         + "</batting_average>\r\n");
        out.write("  </player>\r\n");
      }
      out.write("</players>\r\n");
      out.close();
      in.close();
    }
    catch (IOException e) {
      System.err.println(e);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Usage: java BattingAverage input_file.tab");
    }

  }

  public static String[] splitLine(String playerStats) {

    // count the number of tabs
    int numTabs = 0;
    for (int i = 0; i < playerStats.length(); i++) {
      if (playerStats.charAt(i) == '\t') numTabs++;
    }
    int numFields = numTabs + 1;
    String[] fields = new String[numFields];
    int position = 0;
    for (int i = 0; i < numFields; i++) {
      StringBuffer field = new StringBuffer();
      while (position < playerStats.length()
       && playerStats.charAt(position++) != '\t') {
        field.append(playerStats.charAt(position-1));
      }
      fields[i] = field.toString();
    }
    return fields;

  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000, 2001 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified March 8, 2000