java - 在 Java 中循环输出到 .txt 文件

标签 java file-io while-loop

这是我的代码:

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

public class Main {
public static void main(String[] args){

    ArrayList<Employee> ArrEmployee = new ArrayList<Employee>(); //  array for employee objects

        try {
            Scanner txtIn = new Scanner(new File("payroll.txt"));
            PrintWriter txtOut = new PrintWriter("payrollError.txt");


            while (txtIn.hasNext()) { // looping through the payroll.txt file and creating Employee objects from its data
                try{
                long EmployeeNumber = txtIn.nextLong();
                String EmployeeName = txtIn.next();
                String LastName = txtIn.next();
                double HoursWorked = txtIn.nextDouble();
                double HourlyWage = txtIn.nextDouble();
                if (HourlyWage > 10.35){ 
                    throw new InputMismatchException(); // throws exception if the hourly wage is less than 10.35$
                }
                else
                    ArrEmployee.add(new Employee(EmployeeNumber,EmployeeName,LastName,HoursWorked,HourlyWage)); // creates Employee objects according to the input payroll.txt
                }
                catch (InputMismatchException n) { // catching long,strings and doubles in the payroll.txt that aren't valid
                    txtOut.println(Employee.EmployeeNumber + " " + Employee.EmployeeName + " " + Employee.LastName + " " + Employee.HoursWorked + " " + Employee.HourlyWage);
                    System.out.println("lol");
                      }
                txtOut.close();
            }

        } catch (FileNotFoundException e) {
            System.out.println("File payroll.txt was not found.");

            }

          }

        }

我正在尝试输出到名为 payrollError.txt 的 .txt 文件,但由于某种原因,while 循环无限地进行...

我似乎无法在我的主类中找到问题,任何帮助将不胜感激。

这是我的 payroll.txt 代码:

31718 PHILLIP LENNOX 55.0 20.00
11528 NANCY TROOPER 40.0 10.45
16783 JOHN CONNAUGHT 30.5 10.00
10538 PETER DUNCAN 45.0 10.75
21O15 JAMES HAROLD 32.0 10.50
61326 HARRY KUHN 25.0 12.30
82465 MICHELLE BENOIT 50.0 18.50
31816 DANIELLE RAYMOND 35.5 15.25
73745 JACK O'TOOLE 28.0 11.50
81514 VICTORIA HALL 48.5 17.50
71854 ALI MOHUMAD 35.0 10.75
92012 ALLAN MARS 36.0 15.00
52853 MICHAEL BOONE 60.5 17.50
41714 JULIE HART 25.0 10.50
58342 MIKE HEINZ 28.2 16.85
62080 PIETRO SULA 32.5 11.50
21638 MICHEL RAE 40.5 12.50
52726 MITCHELL HACKETT 23,7 12.05

基本上,它应该像逐行一样循环遍历这些(long,string,string,double,double),找到任何无效的值,例如写为 23,7 而不是 23.7 的 double 值,并将其输出到我的文件 payrollError。 txt。

我的员工类别:

public class Employee {

     public static long EmployeeNumber;
     public static String EmployeeName;
     public static String LastName;
     public static double HoursWorked;
     public static double HourlyWage;

    public Employee(long EmployeeNumber, String EmployeeName, String LastName, double HoursWorked, double HourlyWage ){

        this.EmployeeNumber = EmployeeNumber;
        this.EmployeeName = EmployeeName;
        this.LastName = LastName;
        this.HoursWorked = HoursWorked;
        this.HourlyWage = HourlyWage;
    }
}

最佳答案

不要关闭 while 循环体中的输出。另外,请遵循 Java 变量命名约定。不要显示“lol”,而是尝试输出您正在编写的消息。如果您无法初始化Employee,则很难看出现有方法如何获取已读取的值。最后,您需要 close() 您的 PrintWriter (或使用 try-with-resources 如下所示为您完成此操作)。类似的东西

List<Employee> al = new ArrayList<>();
try (PrintWriter txtOut = new PrintWriter("payrollError.txt")) {
    Scanner txtIn = new Scanner(new File("payroll.txt"));
    while (txtIn.hasNext()) {
        long employeeNumber;
        String employeeName;
        String lastName;
        double hoursWorked;
        double hourlyWage;

        try {
            employeeNumber = txtIn.nextLong();
            employeeName = txtIn.next();
            lastName = txtIn.next();
            hoursWorked = txtIn.nextDouble();
            hourlyWage = txtIn.nextDouble();
            if (hourlyWage > 10.35) {
                throw new InputMismatchException();
            } else {
                al.add(new Employee(employeeNumber, employeeName,
                                lastName, hoursWorked, hourlyWage));
            }
        } catch (InputMismatchException n) {
            String msg = employeeNumber + " " + employeeName + " "
                    + lastName + " " + hoursWorked + " " + hourlyWage;
            txtOut.println(msg);
            System.out.println(msg);
        }
        // txtOut.close(); // <-- this is inside the loop.
    }
} catch (FileNotFoundException e) {
    System.out.println("File payroll.txt was not found.");
}

您的Employee类中的字段不能是static! (我会让它们小写以遵循 Java 命名约定;否则它们看起来像类名)

 private /* static */ long EmployeeNumber;
 private /* static */ String EmployeeName;
 private /* static */ String LastName;
 private /* static */ double HoursWorked;
 private /* static */ double HourlyWage;

将字段设置为私有(private)是封装的一个示例。

关于java - 在 Java 中循环输出到 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115005/

相关文章:

java - While 循环 1 - 100 之间的素数

java - 如何比较两个不同大小的 Csv 文件之间的值?

java - SOAP 消息到 web 服务 - HTTP 响应代码 : 403 for URL

java - JPA数据库中的外键,项目中的返回对象

java - 从 Jar 文件中的资源复制/提取目录

c++ - 打开文件 'write' 比打开文件 'read' 花费的时间多 10-50 倍

java - 一次向字符串添加一行

java - 使用 org.json.simple json 解析器将变量从 while 循环传递到另一个类

java - 使用集合 .txt 文件中的 Map TreeSet 对单选按钮进行分组

java - 返回使用正则表达式找到的字符串