我坚持创建一个循环来一次读取一个文件的 6 行,将每一行保存到一个变量中以供以后用于计算
I am stuck on creating a loop to read 6 lines of a file at a time, saving each line to a variable to be used later for calculations
该项目是分析一个文本数据文件并从中提供统计数据。它是一个固定格式的文件。我们使用读取的数据编写一个新的固定格式文件。我们所做的是读取 First name、Last name、Age、current income、current savings 和 raise。有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
您想一次读完这个文件六行,并根据每组的内容进行计算。首先,你需要一个地方来放置你的值,静态内部 class 会很有用:
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 而不是双精度数,因为我们正在进行财务计算。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;
}
}
}
该项目是分析一个文本数据文件并从中提供统计数据。它是一个固定格式的文件。我们使用读取的数据编写一个新的固定格式文件。我们所做的是读取 First name、Last name、Age、current income、current savings 和 raise。有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
您想一次读完这个文件六行,并根据每组的内容进行计算。首先,你需要一个地方来放置你的值,静态内部 class 会很有用:
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 而不是双精度数,因为我们正在进行财务计算。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;
}
}
}