有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

输入如何在java中读取文件时跳过行

在读取java文件时,如何跳过这一行?每次我运行它,当它转到空行时就会崩溃

该文件包含:

Sue     12.60  8 4 5 8 2

Juan    13.75  4 2 7 5 10

Betty   12.10  9 3 5 7.  
Jin     14.9   8 8 2 3 4 1

以下是我到目前为止的情况:

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

public class DATA {
    public static void main(String[] args) {
        Scanner input = null;

        try {
            input = new Scanner(new File("EmployeeData.txt"));
        } catch (Exception e) {
            System.exit(-1);
        }

        while (input.hasNextLine()) {
            String line = input.nextLine();
            Scanner lineScan = new Scanner(line);
            String name = lineScan.next();
            double rate = lineScan.nextDouble();
            int sum = 0;
            while (lineScan.hasNextInt()) {
                sum = sum + lineScan.nextInt();
            }

            double salary = rate * sum;
            if (salary > 400) {
                double tax = salary - (salary * 0.33);
                System.out.print(name + " worked for a total of: " + sum + " hours at $" + rate);
                System.out.printf(" hours at $ %.2f%n", rate);
                System.out.printf(" an hour for a gross pay of $ %.2f%n", salary);
                System.out.println("\nAfter 33% taxes their total net pay should be " + tax);
                System.out.println();
            } else {
                double tax = salary - (salary * 0.25);
                System.out.print(name + " worked for a total of: " + sum);
                System.out.printf(" hours at $ %.2f", rate);
                System.out.printf(" an hour for a gross pay of $ %.2f", salary);
                System.out.println("\nAfter 25% taxes their total net pay should be $" + tax);
                System.out.println();
            }
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    跳过空行

    String line = input.nextLine();
    
    if (line.isEmpty()) continue;