线程“main”中的异常java.util.NoSuchElementException错误

时间:2014-10-18 02:37:52

标签: java nosuchelementexception

我在执行以下任务时遇到问题:

  

“编写一个程序,以月份日(2000年8月23日)的形式接受任何两个日期,以空格分隔,并计算两个日期之间经过的总天数,包括开始和结束日期请记住,闰年是可被4整除的年份,除了百年之外,必须可以被400整除,例如1600或2000(1800不是闰年)。您可以使用任何类型的日期测试您的程序(或者存储在文件中的日期)但最终必须使用下面显示的数据运行。屏幕上的系统输出是可以接受的。“

到目前为止,我已经有了这段代码并且编译了:

import java.util.Scanner;
import java.io.*;

public class Project3
{
public static void main(String[] args) throws IOException
{

  int m1, d1, y1, m2, d2, y2;
  Scanner scan = new Scanner(new FileReader("dates.txt"));

  for(int i = 0 ; i < 6; ++i)
  {

     m1 = scan.nextInt();
     d1 = scan.nextInt();
     y1 = scan.nextInt();
     m2 = scan.nextInt();
     d2 = scan.nextInt();
     y2 = scan.nextInt();

     System.out.println("The total number of days between the dates you entered are: " + x(m1,    m2, d1, d2, y1, y2));
  }
  } 
   public static int x (int m1, int d1, int y1, int m2, int d2, int y2) 
  {
  int total = 0;
  int total1 = 0;
  int total2 = 0;

  if( m1 == m2 && y1 == y2)     
  {                             
     total = d2 - d1 +1;
  }

  else if ( y1 == y2 )
  {
     total = daysInMonth( m2 , y2 ) - d1 + 1;
  }

  for(int i = m1; i < m2 ; ++i)  
  {
     total1 += daysInMonth( m2, y2 );
  }

  for (int i = y1; i < y2; i++)
  {
     total2 += daysInYear ( y2 );
  }

  total += total1 + total2;
  return total;
 }

//Methods       
 public static boolean isLeap(int yr)
{
  if(yr % 400 == 0)
     return true;
  else if (yr % 4 == 0 && yr % 100 !=0)
     return true;
  else return false;

 }

  public static int daysInMonth( int month , int year)
 {  
  int leapMonth;

  if (isLeap(year) == true)
  {
     leapMonth = 29;
  }
  else
  {
     leapMonth = 28;
  }

  switch(month)
  {
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12: return 31;
     case 4:
     case 6:
     case 9:
     case 11: return 30;
     case 2: return leapMonth;
     }
   return 28;     
  }

 public static int daysInYear(int year)
 { 
  if (isLeap(year))
     return 366;
  else 
     return 365;

 }
}

我遇到的问题是我输出的数据文件中的日期不正确。 我输入了这两个日期7 4 17761 1 1987并获得了723795而不是正确答案76882。 我输入的任何其他日期,它们也是不正确的。

这也是我得到的错误。

  

您输入的日期之间的总天数为:723795
      线程“main”中的异常java.util.NoSuchElementException
      在java.util.Scanner.throwFor(Scanner.java:862)
      在java.util.Scanner.next(Scanner.java:1485)
      在java.util.Scanner.nextInt(Scanner.java:2117)
      在java.util.Scanner.nextInt(Scanner.java:2076)
      在Project3.main(Project3.java:15)

数据文件:

7 
4 
1776        
1 
1 
1987 

拜托,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

首先,当您调用函数x时,您将以错误的顺序传递参数。该函数声明为x(m1,d1,y1,m2,d2,y2),但您调用的是x(m1,m2,d1,d2,y1,y2)。这很难找到。 :)

另外,我已根据原始代码清理了代码。请注意,您需要在循环中使用变量“i”而不是“y1”或“m2”

例如,

for(int i = m1; i < m2 ; ++i)  
{
  total1 += daysInMonth( m2, y2 );
}

此代码没有任何意义。你想使用daysInMonth(i,y2);

这是我的建议:

public static void main(String[] args) throws IOException
{

    int m1, d1, y1, m2, d2, y2;
    Scanner scan = new Scanner(new FileReader("dates.txt"));

    for(int i = 0 ; i < 6; ++i)
    {

        m1 = scan.nextInt();
        d1 = scan.nextInt();
        y1 = scan.nextInt();

        m2 = scan.nextInt();
        d2 = scan.nextInt();
        y2 = scan.nextInt();

        System.out.println("The total number of days between the dates you entered are: " + x(m1, d1, y1, m2, d2, y2));
    }
} 
public static int x (int m1, int d1, int y1, int m2, int d2, int y2)  {
    int total = 0;
    total = d2 - d1 + 1;
    if (y1 == y2) {
        for(int i = m1; i < m2 ; ++i) {
            total += daysInMonth(i, y2);
        }
    } else {
        for (int i = m1; i <= 12; i++) {
            total += daysInMonth(i, y1);
        }
        for (int i = y1+1; i < y2; i++) {
            total += daysInYear(i);
        }
        for (int i = 1; i < m2 ; i++) {
            total += daysInMonth(i, y2);
        }
    }
    return total;
}

结果是:

java Project3
The total number of days between the dates you entered are: 76882

以下是整个代码:

import java.util.Scanner;
import java.io.*;

public class Project3
{
    public static void main(String[] args) throws IOException
  {

      int m1, d1, y1, m2, d2, y2;
      Scanner scan = new Scanner(new FileReader("dates.txt"));

      for(int i = 0 ; i < 6; ++i)
      {

          m1 = scan.nextInt();
          d1 = scan.nextInt();
          y1 = scan.nextInt();

          m2 = scan.nextInt();
          d2 = scan.nextInt();
          y2 = scan.nextInt();

          System.out.println("The total number of days between the dates you entered are: " + x(m1, d1, y1, m2, d2, y2));
      }
  } 
public static int x (int m1, int d1, int y1, int m2, int d2, int y2)  {
    int total = 0;
    total = d2 - d1 + 1;
    if (y1 == y2) {
        for(int i = m1; i < m2 ; ++i) {
            total += daysInMonth(i, y2);
        }
    } else {
        for (int i = m1; i <= 12; i++) {
            total += daysInMonth(i, y1);
        }
        for (int i = y1+1; i < y2; i++) {
            total += daysInYear(i);
        }
        for (int i = 1; i < m2 ; i++) {
            total += daysInMonth(i, y2);
        }
    }
    return total;
}

//Methods       
 public static boolean isLeap(int yr)
{
  if(yr % 400 == 0)
     return true;
  else if (yr % 4 == 0 && yr % 100 !=0)
     return true;
  else return false;

 }

  public static int daysInMonth( int month , int year)
 {  
  int leapMonth;

  if (isLeap(year) == true)
  {
     leapMonth = 29;
  }
  else
  {
     leapMonth = 28;
  }

  switch(month)
  {
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12: return 31;
     case 4:
     case 6:
     case 9:
     case 11: return 30;
     case 2: return leapMonth;
     }
   return 28;     
  }

 public static int daysInYear(int year)
 { 
  if (isLeap(year))
     return 366;
  else 
     return 365;

 }
}