gpt4 book ai didi

java - 文件 IO,未正确检查值并输出到文件?

转载 作者:行者123 更新时间:2023-12-01 09:11:21 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序应该获取一个包含人员和工资值的文本文件,然后将高于 250,000 的工资打印到输出文件 (OVER250000.txt) 并打印到控制台。我正在解析的文档的部分示例是:

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

我尝试将所有内容都编码出来,但由于某种原因,它输出的文件 (OVER250000.txt) 是空的,而且它只是读取并打印出所有人员及其工资,而不是仅读取超过 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();
}

最佳答案

更改此代码(请参阅内联注释)

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++;
}
}

关于java - 文件 IO,未正确检查值并输出到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com