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

Using Java, I am trying to read in a file with the format outlined below. \"AARO

ID: 3848783 • Letter: U

Question

Using Java, I am trying to read in a file with the format outlined below.


"AARON, ELVIA J"   WATER RATE TAKER   WATER MGMNT   "$87,228.00"
"AARON, JEFFERY M"   POLICE OFFICER   POLICE   "$75,372.00"
"AARON, KARINA"   POLICE OFFICER   POLICE   "$75,372.00"
"AARON, KIMBERLEI R"   CHIEF CONTRACT EXPEDITER   GENERAL SERVICES   "$80,916.00"
"ABAD JR, VICENTE M"   CIVIL ENGINEER IV   WATER MGMNT   "$99,648.00"
"ABARCA, ANABEL"   ASST TO THE ALDERMAN   CITY COUNCIL   "$70,764.00"
"ABARCA, EMMANUEL"   GENERAL LABORER - DSS   STREETS & SAN   "$40,560.00"
"ABBATACOLA, ROBERT J"   ELECTRICAL MECHANIC   AVIATION   "$89,440.00"
"ABBATEMARCO, JAMES J"   FIRE ENGINEER   FIRE   "$84,396.00"
"ABBATE, TERRY M"   POLICE OFFICER   POLICE   "$80,724.00"
"ABBOTT, BETTY L"   FOSTER GRANDPARENT   FAMILY & SUPPORT   "$2,756.00"
"ABBOTT, LYNISE M"   CLERK III   POLICE   "$41,784.00"
"ABBRUZZESE, WILLIAM J"   INVESTIGATOR - IPRA II   IPRA   "$65,808.00"
"ABDALLAH, ZAID"   POLICE OFFICER   POLICE   "$65,016.00"
"ABDELHADI, ABDALMAHD"   POLICE OFFICER   POLICE   "$75,372.00"
"ABDELLATIF, AREF R"   FIREFIGHTER (PER ARBITRATORS AWARD)-PARAMEDIC   FIRE   "$90,738.00"
"ABDELMAJEID, AZIZ"   POLICE OFFICER   POLICE   "$75,372.00"
"ABDOLLAHZADEH, ALI"   PARAMEDIC I/C   FIRE   "$81,672.00"
"ABDUL-KARIM, MUHAMMAD A"   ENGINEERING TECHNICIAN VI   WATER MGMNT   "$96,384.00"
"ABDULLAH, ASKIA"   LEGISLATIVE AIDE   CITY COUNCIL   "$25,008.00"

The problem I am having is in splitting the file between arrays. The first array has the name example: AARON, ELVIA J ( with or without the comma makes no difference). The second array hold the department and position example: WATER RATE TAKER   WATER MGMT. The third array holds the salary as a double example: 87228.00. The code I have so far is below, I believe my error is happening between lines 33-35. I need to know how to fix the error and what I am doing wrong. The arrays also need to line up so I can match the index places with the correct records example index 0 in each array contains the information for the same person.

import java.io.*;
import java.util.*;
public class PA3 {
    String name;
    String position;
    double salary;

    public PA3(String name, String position, double salary){
        this.name = name;
        this.position = position;
        this.salary = salary;
    } // end constructor

    public static void main(String[] args) {
        ArrayList<PA3> employee = new ArrayList<>();
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("empl.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                System.out.println (strLine);
                // split the data by the quotation marks int three separate arrays using string tokenizer
                String[] tokenize = strLine.split(""");
                String tempName = tokenize[1]; // name is split and moved to array
                String tempPosition = tokenize[2]; // position is split and moved to array
                double tempSalary = Double.parseDouble((tokenize[3].replace("$","")).replace(",",""));
                PA3 tempObject = new PA3(tempName, tempPosition, tempSalary);
                employee.add(tempObject);
                } // end while
        //Close the input stream
            in.close();
            } // end try
        catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
            } // end catch
        for (PA3 each : employee) {
        System.out.println("==========================");
        System.out.println(each);
        System.out.println();
        } // end for
    } //end main
} // end class

Thank You!

Explanation / Answer

Hi, commented lines are new changes. // ------

I tried it in junit test and now it is working fine. You can remove junit test and put main function to run this.

import org.junit.Test;

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

public class perWithW {

   @Test
   public void test() throws Exception {
       ArrayList<PA3> employee = new ArrayList<PA3>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("C:\event ead.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
// split the data by the quotation marks int three separate arrays using string tokenizer
String[] tokenize = strLine.split(" "); //--- First split with 4 spaces
String tempName = tokenize[1]; // name is split and moved to array
String tempPosition = tokenize[2]; // position is split and moved to array
String temp = (tokenize[3].replace("$","").replace(",", "").replace(""", "")); //----- Replace all unwanted characters
double tempSalary = Double.parseDouble(temp);
PA3 tempObject = new PA3(tempName, tempPosition, tempSalary);
employee.add(tempObject);
} // end while
//Close the input stream
in.close();
} // end try
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
} // end catch
for (PA3 each : employee) {
System.out.println("==========================");
System.out.println(each);
System.out.println();
} // end for
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote