Christopher Miller
Christopher Miller

Reputation: 25

File IO, not checking values correctly & outputting to file?

I'm working on a program that is supposed to take a text file filled with people and salary values, and then print out the salaries that are above 250,000 to an output file (OVER250000.txt) and also print to console. A part example of the document I am parsing is:

Agency  Lastname    Firstname   Middlename  JobTitle    BasePay OtherComp   TotalComp
CLEMSON UNIVERSITY  MCCORMICK   ROBERT      DEAN    250000  0   250000
CLEMSON UNIVERSITY  ELLIOTT ANTONIO     ATHLETICS COACH 249900  0   249900
CLEMSON UNIVERSITY  SMITH   AUDRA       ATHLETICS COACH 249900  0   249900
CLEMSON UNIVERSITY  REED    MICHAEL     ATHLETICS COACH 249900  0   249900
CLEMSON UNIVERSITY  SCOTT   JEFFREY     ATHLETICS COACH 249900  0   249900
MEDICAL UNIVERSITY OF SC    SHAW    DARLENE     ASSOCIATE PROVOST   247881  0    76195
MEDICAL UNIVERSITY OF SC    LEITE   LUIS        DEPARTMENT CHAIR/HEAD   246474  0   246474
MEDICAL UNIVERSITY OF SC    BARRY   JOHN        ASSOCIATE DEAN  246353  0   246353

I tried to code everything out but for some reason the file it outputs (OVER250000.txt) is empty and also it just reads and prints out all of the people with their salaries instead of only people over 250,000**

public class SalaryAnalyzer {
static final String IN_FILE_NAME = "StateOfSC-Salary-List-04012015.txt";
static final String OUT_FILE_NAME = "OVER250000.txt";
static final String DELIM = "\t";

public static void main(String[] args) throws IOException {
    System.out.println("Let's see how many state employees make over $250,000 and work at USC.");
    analyzeEmployeeFile(IN_FILE_NAME);
    System.out.println("Results have been printed to " + OUT_FILE_NAME);
}

public static void analyzeEmployeeFile(String fileName) throws IOException {
    String text = "";
    int noLines = 0;
    //Create a buffer reader object to read file
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    //Ignore first line of file
    String line = br.readLine();
    //Read from the second line onwards
    while ((line = br.readLine()) != null) {
        String strs[] = line.split(DELIM);
        System.out.println(line);
        //Check if salary is greater than 250,000

        if (Double.parseDouble(strs[6]) > 250000) {
            text = text + "\n" + line;
            noLines++;
        }
    }
    printToSalaryFile(OUT_FILE_NAME, text);
}

//Will print contents to output file
public static void printToSalaryFile(String fileName, String text) throws FileNotFoundException {
    //Create a PrintWriter object to write data to output file
    PrintWriter writeToFile = new PrintWriter(new File(fileName));
    String lines[] = text.split("\n");
    //Print all records
    for (int i = 0; i < lines.length; i++) {
        System.out.println(lines[i]);
        writeToFile.println(lines[i]);
    }
    writeToFile.close();
}

Upvotes: 0

Views: 34

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

change this code (see inline comments)

while ((line = br.readLine()) != null) {
    String strs[] = line.split(DELIM);
    System.out.println(line);        // comment this line if you don't want it printed to console
    //Check if salary is greater than 250,000

    if (Double.parseDouble(strs[5]) > 250000) {  // arrays are zero based so change to 5  
                                                 // or if this is total package then index is 7
        text = text + "\n" + line;
        noLines++;
    }
}

Upvotes: 1

Related Questions