0

私はしばらくこの宿題で立ち往生しています。基本的に、各行に 1 つの日付で構成されるファイルを読み取る日付のコンストラクターを作成する必要があります。各日付は Date オブジェクトに格納する必要があります。

ただし、「System.out.println(date);」に似た「日付: 1997 年 6 月 17 日」のように、それを読んだ後に日付を出力する必要があります。

したがって、私が抱えている問題は、各行を適切に反復処理できることです。テキスト ファイルのすべての行をスキャンする while ループがありますが、完了すると最後の行にしかアクセスできません。各行に 1 つずつ順番にアクセスできるようにコーディングするにはどうすればよいですか?

興味のある方には、これが課題です。

これまでの私のコードは次のとおりです。私がやろうとしていることを説明するのに十分に明確でない場合は申し訳ありません。私はこれが初めてです。

public class Date {

private int month; // 1-12
private int monthNum; // number of month
private int day; // 1-31 varies by month
private int year; // any year
private String chkMonth, chkDay;
private String theMonth;
private int theYear,valid;

/**
 * days in each month with 0 at the index since there is no month "zero"
 */
private static final int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31,
        31, 30, 31, 30, 31 };

/**
 * constructor: call checkMonth to confirm proper value for month; call
 * checkDay to confirm proper value for day
 * 
 * @param theMonth
 *            the month of the year
 * @param theDay
 *            the day of the month
 * @param theYear
 *            the year
 */

public Date() {
    Scanner in = null;
    try {
        in = new Scanner(new File("dates.txt"));
    } catch (FileNotFoundException exception) {
        System.err.println("failed to open dates.txt");
        System.exit(1);
    }

    while (in.hasNextLine()) {
        theMonth = in.next();
        chkMonth = theMonth.replaceAll("\\.", "");
        chkMonth = chkMonth.toLowerCase();
        chkDay = in.next();
        chkDay = chkDay.replaceAll("\\,", "");
        day = Integer.parseInt(chkDay);
        theYear = in.nextInt();
        // need more code for DateRange objects
        valid = 1;
        year = theYear; // could validate year
        if (checkMonth(chkMonth) == 0)
            valid = 0; // validate month

        if ((checkDay(day)) == 0)
            valid = 0; // validate day

        System.out.println(this);

    }

}



/**
 * utility method to confirm proper month value
 * 
 * @param theMonth
 * @return testMonth or throw an IllegalArgumentException
 */
private int checkMonth(String theMonth) {
    if (((theMonth.compareTo("jan")) == 0)
            || ((theMonth.compareTo("january")) == 0))
        monthNum = 1;
    else if (((theMonth.compareTo("feb")) == 0)
            || ((theMonth.compareTo("february")) == 0))
        monthNum = 2;
    else if (((theMonth.compareTo("mar")) == 0)
            || ((theMonth.compareTo("march")) == 0))
        monthNum = 3;
    else if (((theMonth.compareTo("apr")) == 0)
            || ((theMonth.compareTo("april")) == 0))
        monthNum = 4;
    else if (((theMonth.compareTo("may")) == 0))
        monthNum = 5;
    else if (((theMonth.compareTo("june")) == 0))
        monthNum = 6;
    else if (((theMonth.compareTo("july")) == 0))
        monthNum = 7;
    else if (((theMonth.compareTo("aug")) == 0)
            || ((theMonth.compareTo("august")) == 0))
        monthNum = 8;
    else if (((theMonth.compareTo("sept")) == 0)
            || ((theMonth.compareTo("september")) == 0))
        monthNum = 9;
    else if (((theMonth.compareTo("oct")) == 0)
            || ((theMonth.compareTo("october")) == 0))
        monthNum = 10;
    else if (((theMonth.compareTo("nov")) == 0)
            || ((theMonth.compareTo("november")) == 0))
        monthNum = 11;
    else if (((theMonth.compareTo("dec")) == 0)
            || ((theMonth.compareTo("december")) == 0))
        monthNum = 12;

    if (monthNum > 0 && monthNum <= 12) // validate month
        return monthNum;
    else
        return monthNum = 0;
    /*
     * else // month is invalid throw new
     * IllegalArgumentException("month must be 1-12");
     */
}

/**
 * utility method to confirm proper day value based on month and year
 * 
 * @param testDay
 * @return testDay or throw an IllegalArgumentException
 */
private int checkDay(int testDay) {
    // check if day in range for month
    if (testDay > 0 && testDay <= daysPerMonth[monthNum])
        return testDay;

    // check for leap year
    if (monthNum == 2 && testDay == 29
            && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
        return testDay;

    // System.out.println("Invalid Date");

    return 0;

}

/**
 * return a String of the form month/day/year
 * 
 * @return a String of the form month/day/year
 */
public String toString() {
    if (valid == 1) {
        String ret = "Date: ";
        ret += theMonth + " ";
        ret += day + ", ";
        ret += year;
        return ret;
    } else
        return "Date Invalid";

}

}

4

1 に答える 1

0

ファイルを処理するためのコードは、Date クラスではなくLab3クラスにある必要があります。さらに、 Date コンストラクターには次のようなパラメーターが必要です。

public Date(theMonth, theDay, theYear) {
    this.year = theYear;
    this.month = theMonth;
    this.day = theDay;
}

次に、Lab3 クラスで、次のようにして新しい Date オブジェクトを作成できます。

Date nextDate = new Date(in.next(), in.next(), in.nextInt());

もちろん、それよりももう少しエラーチェックを行う必要があります。

作成したすべての Date オブジェクトを ArrayList などのどこかに保存することも役立ちます。

于 2013-09-23T03:14:21.350 に答える