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

Robbie Beatty :

The project is to analyze a text data file and provide statistics from it. It is a fixed format file. We write a new fixed format file using data we read. What we do is read the First name, Last name , Age, current income, current savings , and raise. There is 606 lines of code. So we have to read the first 6 lines, then loop reading 6 lines and storing the data. Then we have to find the number of individuals by a few age groups (ex 20-29, 30-39, ect). Find the min max and average of current income. Min max and average of total savings. Retirement Income (Monthly) min max and average. Retirement Average income per age bracket (ex, 20-29, 30,39, ect). I have been going over my textbook (big java, late objects) for the past 2 hours now and going through my past assignments and I am at a complete loss on where to start. I am a first year student in university for java programming. We have not used arrays yet. Where do I start on this? I understand what I need to do but I dont understand reading from files in any way. From research I have done on the internet there are so many different ways that people do that I have never even seen before.

Current Code (Please note I am very lost in this)

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
Bill Horvath :

You want to read through this file six lines at a time, and do calculations based on the contents of each set. First, you need a place to put your values, and a static inner class would be useful:

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

Now you have a useful data structure into which you can put the lines your scanner is reading. (Note we're using BigDecimals instead of doubles, because we're doing financial calculations. 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);
    }

You're now set up to do your calculations, which I'd put into appropriately named methods:

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

And you'll need to specify the constants being used:

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);

So what do you think? Does this give you enough of a start? Here's the entire program so far; I've added annotations to explain in more detail what's going on:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=385108&siteId=1