我一直坚持创建一个循环以一次读取文件的6行,将每一行保存到一个变量中以供以后进行计算使用

问题描述 投票:0回答:1

该项目将分析文本数据文件并从中提供统计数据。这是一个固定格式的文件。我们使用读取的数据编写一个新的固定格式文件。我们要做的是读取名字,姓氏,年龄,当前收入,当前储蓄和加薪。有606行代码。因此,我们必须读取前6行,然后循环读取6行并存储数据。然后,我们必须按几个年龄段(例如20-29岁,30-39岁等)找到人数。找到当前收入的最小最大值和平均值。总节省的最小值,最大值和平均值。退休收入(每月)最小值最大值和平均值。退休年龄段的平均收入(例如20-29岁,30.39等)。现在,我已经过去2个小时浏览了我的教科书(大型Java,后期对象),并完成了过去的作业,我从哪里开始完全迷失了方向。我是大学一年级的Java编程学生。我们尚未使用数组。我从哪里开始呢?我了解我需要做什么,但我不了解以任何方式读取文件。根据我在互联网上所做的研究,人们做过很多不同的事情,这是我以前从未见过的。

当前代码(请注意,我对此非常迷失)

import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MidtermProject {

    public static void main(String[] args) throws FileNotFoundException {

    // Declare the file 
    File inputFile = new File ("midterm.txt");
    Scanner in = new Scanner(new File("midterm.txt"));

    // Read first 6 lines 
    String One = in.nextLine();
    String Two = in.nextLine();
    String Three = in.nextLine();
    String Four = in.nextLine();
    String Five = in.nextLine();
    String Six = in.nextLine();

    // Set up loop to read 6 lines at a time  
    }
}
Example of some of the text file 
FirstName
LastName
Age
currentIncome
CurrentSavings
Raise
ROSEMARY
SAMANIEGO
    40
    81539.00
    44293.87
    0.0527
JAMES
BURGESS
    53
    99723.98
    142447.56
    0.0254
MARIBELL
GARZA
    45
    31457.83
    92251.22
    0.0345
java text-files filereader filewriter
1个回答
1
投票

您想一次通读该文件六行,并根据每组的内容进行计算。首先,您需要一个放置值的位置,并且静态内部类将很有用:

private static class EmployeeData {
    final String firstName;
    final String lastName;
    final int age;
    final BigDecimal income;
    final BigDecimal savings;
    final BigDecimal raise;

    public EmployeeData(String firstName, String lastName, int age, BigDecimal income, BigDecimal savings, BigDecimal raise) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.income = income;
        this.savings = savings;
        this.raise = raise;
    }
}

现在您有了一个有用的数据结构,可以在其中放入扫描仪正在读取的行。 (请注意,由于我们正在执行财务计算,因此我们使用BigDecimals而不是double。That matters。)

    Scanner in = new Scanner(new File("midterm.txt"));

    while (in.hasNext()){

        String firstName = in.nextLine();
        String lastName = in.nextLine();
        int age = in.nextInt();
        BigDecimal income = in.nextBigDecimal();
        BigDecimal savings = in.nextBigDecimal();
        BigDecimal raise = in.nextBigDecimal();

        EmployeeData employeeData = new EmployeeData(firstName, lastName, age, income, savings, raise);
        BigDecimal power = calculatePower(employeeData);
    }

您现在已准备好进行计算,我已将其放入适当命名的方法中:

private static BigDecimal calculatePower(EmployeeData employeeData){
    int ageDifference = RETIREMENT_AGE - employeeData.age;
    BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
    BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(), ageDifference));
    return employeeData.income.multiply(baseRaised);
}

并且您需要指定要使用的常量:

private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
private static final int RETIREMENT_AGE = 70;
private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);

所以您怎么看?这是否给您足够的开始?到目前为止,这是整个程序;我添加了注释以更详细地说明发生了什么:

import java.math.BigDecimal;
import java.util.*;

// The file name will have to match the class name; e.g. Scratch.java
public class Scratch {

    // These are values that are created when your class is first loaded. 
    // They will never change, and are accessible to any instance of your class.
    // BigDecimal.valueOf 'wraps' a double into a BigDecimal object.
    private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
    private static final int RETIREMENT_AGE = 70;
    private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
    private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);

    public static void main(String[] args) {
        // midterm.txt will have to be in the working directory; i.e., 
        // the directory in which you're running the program 
        // via 'java -cp . Scratch'
        Scanner in = new Scanner(new File("midterm.txt"));

        // While there are still more lines to read, keep reading!
        while (in.hasNext()){

            String firstName = in.nextLine();
            String lastName = in.nextLine();
            int age = in.nextInt();
            BigDecimal income = in.nextBigDecimal();
            BigDecimal savings = in.nextBigDecimal();
            BigDecimal raise = in.nextBigDecimal();

            // Put the data into an EmployeeData object
            EmployeeData employeeData = new EmployeeData(firstName, lastName, age, income, savings, raise);
            // Calculate power (or whatever you want to call it)
            BigDecimal power = calculatePower(employeeData);
            System.out.println("power = " + power);
        }
    }

    private static BigDecimal calculatePower(EmployeeData employeeData){
        int ageDifference = RETIREMENT_AGE - employeeData.age;
        // With big decimals, you can't just use +, you have to use 
        // .add(anotherBigDecimal)
        BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
        BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(), ageDifference));
        return employeeData.income.multiply(baseRaised);
    }
    // A static inner class to hold the data
    private static class EmployeeData {

        // Because this class is never exposed to the outside world, 
        // and because the values are final, we can skip getters and 
        // setters and just make the variable values themselves accessible 
        // directly by making them package-private (i.e., there is no '
        // private final' or even 'public final', just 'final')
        final String firstName;
        final String lastName;
        final int age;
        final BigDecimal income;
        final BigDecimal savings;
        final BigDecimal raise;


        public EmployeeData(String firstName, String lastName, int age, BigDecimal income, BigDecimal savings, BigDecimal raise) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.income = income;
            this.savings = savings;
            this.raise = raise;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.