私は、変数に各行を節約、一度にファイルの6行を読み取るためにループを作成する上で立ち往生していた計算のために、後で使用するために

ロビー・ビーティ:

プロジェクトは、テキストデータファイルを分析し、そこから統計情報を提供することです。これは、固定形式のファイルです。私たちは、読み取ったデータを使用して、新しい固定フォーマットのファイルを書き込みます。私たちがやっていることは、名、姓、年齢、現在の収入、現在の貯蓄、および昇給を読んでいます。コードの606行があります。だから我々は、ループが6行を読み取り、データを格納し、最初の6行を読まなければなりません。その後、我々はいくつかの年齢グループ(例:20-29、30-39、電気ショック療法)によって個体数を見つける必要があります。現在の収入の分maxと平均して下さい。最小最大総貯蓄の平均。退職所得(月額)分maxと平均。年齢層ごとの退職の平均所得(例、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
ビルHorvathの:

一度、このファイルを使用して6行を読んで、各セットの内容に基づいて計算を行いたいです。まず、あなたは自分の価値観を置く場所が必要、と静的内部クラスは有用であろう。

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

今あなたがあなたのスキャナが読んでいる行を置くことができるそこに有用なデータ構造を有しています。(私たちは財務計算をやっているので、我々は、代わりにdoubleののBigDecimalを使用しています。その事項を。)

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=385503&siteId=1