Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-将行扫描到构造函数中_Java_Constructor_Java.util.scanner - Fatal编程技术网

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

Java-将行扫描到构造函数中,java,constructor,java.util.scanner,Java,Constructor,Java.util.scanner,我做这个作业已经有一段时间了。基本上,我必须为读取文件的日期创建一个构造函数,每行包含一个日期。每个日期都应存储在日期对象中 然而,我必须在阅读后打印出日期,例如“日期:1997年6月17日”,类似于“System.out.println(date);” 因此,我的问题是能够正确地迭代每一行。我有一个while循环,它将扫描文本文件中的所有行,但只有最后一行可以访问。如何对其进行编码,以便可以依次访问每一行 对于那些感兴趣的人来说,这是 这是我到目前为止的代码,很抱歉,如果我没有足够清楚地解释我

我做这个作业已经有一段时间了。基本上,我必须为读取文件的日期创建一个构造函数,每行包含一个日期。每个日期都应存储在日期对象中

然而,我必须在阅读后打印出日期,例如“日期:1997年6月17日”,类似于“System.out.println(date);”

因此,我的问题是能够正确地迭代每一行。我有一个while循环,它将扫描文本文件中的所有行,但只有最后一行可以访问。如何对其进行编码,以便可以依次访问每一行

对于那些感兴趣的人来说,这是

这是我到目前为止的代码,很抱歉,如果我没有足够清楚地解释我正在尝试做什么,我是新手

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

}
公开课日期{
private int month;//1-12
private int monthNum;//月数
private int day;//1-31按月变化
private int year;//任何年份
私有字符串chkMonth,chkDay;
每月私人字符串;
私用年,有效;
/**
*指数为0的每月天数,因为没有月份“0”
*/
私有静态final int[]daysPerMonth={0,31,28,31,30,31,30,31,
31, 30, 31, 30, 31 };
/**
*构造函数:调用checkMonth以确认月份的正确值;调用
*检查day以确认day的正确值
* 
*@param theMonth
*月份
*@param theDay
*月日
*@param theYear
*年度
*/
公开日期(){
扫描仪输入=空;
试一试{
in=新扫描仪(新文件(“dates.txt”);
}捕获(FileNotFoundException异常){
System.err.println(“无法打开dates.txt”);
系统出口(1);
}
while(在.hasNextLine()中){
theMonth=in.next();
chkMonth=theMonth.replaceAll(“\\”,“);
chkMonth=chkMonth.toLowerCase();
chkDay=in.next();
chkDay=chkDay.replaceAll(“\\,”和“”);
day=Integer.parseInt(chkDay);
年份=in.nextInt();
//需要更多DateRange对象的代码吗
有效=1;
year=theYear;//可以验证year
如果(检查月份(检查月份)=0)
valid=0;//验证月份
如果((检查日(日))==0)
valid=0;//验证日期
System.out.println(本文件);
}
}
/**
*确定正确月份值的实用方法
* 
*@param theMonth
*@return testMonth或抛出IllegalArgumentException
*/
private int checkMonth(字符串每月){
如果((与“一月”相比的第个月)=0)
||((与(“一月”)相比的月份)==0))
月数=1;
如果((与“二月”)相比的月份)=0)
||((与二月比较的月份)==0))
月数=2;
如果((与(“三月”)比较的月份)==0)
||((与三月比较的月份)==0))
月数=3;
如果((与(“apr”)比较的月份)==0,则为else
||((与四月比较的月份)==0))
月数=4;
否则,如果((与(“五月”)相比的第个月)=0))
月数=5;
否则,如果((与六月比较的月份)==0))
月数=6;
否则,如果((与“7月份”相比的月份)==0))
月数=7;
如果((与(“八月”)相比的月份)==0)
||((与八月比较的月份)==0))
月数=8;
如果((与(“9月”)相比的第个月)=0)
||((与(“九月”)相比的月份)==0))
月数=9;
如果((与“十月”相比的第个月)=0)
||((与10月份相比的月份)==0))
月数=10;
如果((与(“11”)相比的月份)==0)
||(与十一月比较的月份)==0)
月数=11;
如果((与“十二月”比较的月份)==0)
||((与(“12月”)相比的月份)==0)
月数=12;

如果(monthNum>0&&monthNum 0&&testDay处理文件的代码不应该在Date类中,而是在Lab3类中。此外,日期构造函数应该有如下参数:

public Date(theMonth, theDay, theYear) {
    this.year = theYear;
    this.month = theMonth;
    this.day = theDay;
}
然后,在Lab3类中,可以通过执行以下操作创建新的日期对象:

Date nextDate = new Date(in.next(), in.next(), in.nextInt());
当然,您需要做更多的错误检查


它还可以帮助存储您在某处创建的所有日期对象,例如在ArrayList中。

谢谢,这至少让我知道了如何构造它。我尝试过从Lab03类中读取它。但是我对如何存储日期对象的想法迷茫了。我似乎找不到如何创建ArrayList对象。我尝试过日期[i] =新建日期(参数),但在从扫描仪中读取参数时,尚未找到创建日期ArrayList的正确方法。在读取文件之前,应创建并初始化ArrayList。
ArrayList myDates=new ArrayList()
例如,它最初是空的,但是对于您读入的每个日期对象,您都可以执行
myDates.add(nextDate)
我还应该说,ArrayList与ArrayList对比的原因是,在这种情况下,你不知道文件中有多少日期,对吗?所以如果你使用的是数组,那么它有多大?我希望这能更清楚一点。谢谢,我会研究ArrayList,这样听会更有意义。如果我有足够的声誉,我会提升它投票吧,因为我认为仅仅是感谢一个人的评论是不允许的。对于发帖礼仪来说,这还是个新鲜事。