简体   繁体   English

Java-将行扫描到构造函数中

[英]Java - scanning lines into a constructor

I've been stuck on this homework for a while. 我已经在这项作业上停留了一段时间。 Basically I have to create a constructor for dates that reads a file, which consists of one date on each line. 基本上,我必须为读取文件的日期创建一个构造函数,该文件的每一行都包含一个日期。 Each date should be stored in a Date object. 每个日期都应存储在Date对象中。

However, I have to print out the date after reading it as so "Date: June 17, 1997" with something similar to "System.out.println(date);". 但是,我必须在读取日期后将其打印为“日期:1997年6月17日”,类似于“ System.out.println(date);”。

So the problem I have is being able to iterate through each line properly. 因此,我遇到的问题是能够正确地遍历每一行。 I have a while loop that will scan all the lines in the text file but only the last line is accessible once it's done. 我有一个while循环,它将扫描文本文件中的所有行,但是一旦完成,只有最后一行可以访问。 How do I code it so that I can access each line one at a time sequentially? 如何进行编码,以便可以依次一次访问每一行?

For those of you interested this is the assignment. 对于那些您感兴趣的人,这是作业。

Here is my code so far and sorry if I wasn't clear enough in explaining what I'm trying to do, I'm new to this. 到目前为止,这是我的代码,如果对说明的理解不够清楚,这是我的新手。

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

}

} }

Your code for processing the file should not be in the Date class, but in the Lab3 class. 您的处理文件的代码应该在Date类,但在类Lab3的 Moreover, your Date constructor should have parameters, like so: 此外,您的Date构造函数应具有参数,如下所示:

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

Then in your Lab3 class, you can create a new Date object by doing the following: 然后,在Lab3类中,您可以通过执行以下操作创建一个新的Date对象:

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

Of course, you'd need to do a bit more error-checking than that. 当然,您需要做更多的错误检查。

It would also help to store all the Date objects you've created somewhere, such as in an ArrayList. 这也将有助于将您创建的所有Date对象存储在某个位置,例如ArrayList中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM