Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ANY HELP/INSIGHT ABOUT JAVA IS WELCOME!!!!!!! •A local baseball league collected

ID: 3691384 • Letter: A

Question

ANY HELP/INSIGHT ABOUT JAVA IS WELCOME!!!!!!!

•A local baseball league collected stats on their players. They have a master file of current data stored in a file named data2.txt They have a transaction file that needs to be used to update the master stored in a file named data1.txt

•The fields in the files are First Name, Last Name, Hits and At Bats. The fields are comma delimited

•In addition the transaction file has a transaction code of either (A)dd, (C)hange or (D)elete in the first column

•Both files are sorted by last name then first name

•The league would like to process the adds, changes and deletes from the transaction file to create a new master file.

•The new file should contain the same fields (comma delimited) as the original files

•The new file should also contain the batting average (hits/at bats) for each player

•The new file should remain sorted by last name

•You do not need to do user data validation, simply meet the stated requirements.

data1.txt

data2.txt

Explanation / Answer

package assignment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class BaseBallTransFromFile {

   public static void main(String[] args)
       {
       String transFile= "D:/ravi/Cheg/data1.txt";
       String masterFile = "D:/ravi/Cheg/data2.txt";
       String tempFile = "D:/ravi/Cheg/data_tmp.txt";
       ArrayList<String> lines = new ArrayList<String> ();
       try
           {
               Scanner sc = new Scanner(new File(transFile));
               while(sc.hasNextLine())
               {
                  lines.add(sc.nextLine());
               }
             
               Scanner masterSc = new Scanner(new File(masterFile));
               BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
               String str = null;
               String tokens[] = null;
               String tokens2[] = null;
               boolean found = false;
               while(masterSc.hasNextLine())
               {
                   str = masterSc.nextLine();
                   tokens = str.split(",");
                   if(tokens != null && tokens.length > 2) {
                       found = false;
                        for(String trans: lines) {
                           //check firstname and last name matches
                           tokens2 = trans.split(",");
                           if(tokens2[1].equals(tokens[0]) && tokens2[2].equals(tokens[1])) {
                               found = true;
                               if(tokens2[0].equals("A"))
                                   str += " "+trans+" ";
                               else if(tokens2[0].equals("D"))
                                   str = null;
                               else if(tokens2[0].equals("C")) {
                                   str = tokens[0]+","+tokens[1]+","+tokens2[3]+","+tokens2[4];
                               }
                               break;
                           }
                        }
                        if(str != null)
                           bw.write(str+" ");
                   }
               }
               bw.close();
             
           }
           catch(IOException ex)
           {
               System.out.println("IOException error message: " + ex.getMessage());
           }
       }
}