【问题标题】:Exception in thread "main" java.util.NoSuchElementException Error线程“main”中的异常 java.util.NoSuchElementException 错误
【发布时间】:2014-10-18 02:37:52
【问题描述】:

我在完成以下作业时遇到问题:

"编写一个程序,接受任意两个日期,形式为月日年(8 23 2000),用空格隔开,并计算两个日期之间经过的总天数,包括开始和结束天。请记住,闰年是可以被 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 

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

【问题讨论】:

  • @SotiriosDelimanolis:我已经清理了他的问题标题,去掉了不必要的词。
  • @HovercraftFullOfEels 谢谢。它不允许我更改标题。
  • 你的数据文件是什么样的?
  • @HovercraftFullOfEels 这些数字分别写在彼此下方的不同行 7 4 1776 1 1 1987
  • Somone,请发布您的问题的数据文件。

标签: java nosuchelementexception


【解决方案1】:

首先,当您调用函数 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;

 }
}

【讨论】:

  • 嘿!听起来不错,感谢您发现我的错误。我有另一个问题。奇怪的是,当我复制并粘贴您的建议时,它不会编译!这是我得到的错误:“ Project3.java:45:错误:解析时到达文件结尾}”
  • 您在复制粘贴代码时可能错过了最后一个“}”?
  • 当我添加另一个“}”时,我收到此错误 Project3.java:31: error: cannot find symbol total += daysInMonth(i, y2); ^ 符号:方法 daysInMonth(int,int) 位置:类 Project3
  • 让我添加整个编译没有问题的代码。
  • 好的,我得到了数据文件中的所有内容并且它编译了唯一的事情是它给了我输出并且这个错误仍然存​​在:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多