2013-09-23 22 views
0

我一直在這個作業上停留了一段時間。基本上我必須爲讀取文件的日期創建一個構造函數,該文件由每行上的一個日期組成。每個日期都應該存儲在一個Date對象中。Java - 將線條掃描到構造函數中

但是,我必須在閱讀日期後打印日期,如「日期:1997年6月17日」,類似於「System.out.println(date);」。

所以我的問題是能夠正確迭代每一行。我有一個while循環,它將掃描文本文件中的所有行,但只有最後一行完成後纔可訪問。我如何編寫代碼,以便可以按順序逐一訪問每一行?

對於那些你感興趣,這是assignment.

這是到目前爲止我的代碼和抱歉,如果我沒有解釋什麼,我試圖做的不夠清楚,我是新來這。

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

} 

}

回答

0

您的代碼用於處理文件應該在Date類,但在Lab3的類。此外,您的日期的構造應該有參數,就像這樣:

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中。

+0

謝謝,至少給了我一個關於如何構建它的想法。我曾嘗試從我的Lab03課程中讀取它。然而,我迷失於關於如何存儲日期對象的想法。我似乎無法找到如何製作ArrayList對象。我試過Date date [i] = new Date(parameters),但是在從Scanner中讀取參數時沒有找到創建日期的ArrayList的正確方法。 – Serafel

+0

您應該在讀取文件之前創建並初始化ArrayList。 'ArrayList myDates = new ArrayList ();'例如。它最初是空的,但是對於讀入的每個Date對象,您可以執行'myDates.add(nextDate);' –

+0

我也應該說,ArrayList與數組的原因是在這種情況下,您真的不知道文件中有多少日期,對吧?所以如果你使用一個數組,你有多大?我希望澄清一點。 –