Exception in thread "main" java.util.NoSuchElementException Error

I am having trouble with the following assignment:

"Write a program to accept any two dates in the form of month day year (8 23 2000), separated by spaces, and calculate the total number of days that has elapsed between the two dates, inclusive of the beginning and ending days. Remember that leap year is a year divisible by 4, except for centennial years, which must be divisible by 400, such as 1600 or 2000 (1800 is not a leap year). You can test your program with any typed in dates (or dates stored in a file) but it must finally run with the data shown below. System output on the screen is acceptable."

I have this code so far and it compiles:

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;

 }
}

The problem I am having is my output for the dates in the data file are incorrect. I input these two dates 7 4 1776 and 1 1 1987 and get 723795 instead of the correct answer 76882 . Any other dates I input, they are also incorrect.

This is the error I get as well.

The total number of days between the dates you entered are: 723795
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Project3.main(Project3.java:15)

The data file:

7 
4 
1776        
1 
1 
1987 

Please, any help is greatly appreciated!


First of all, when you call the function x, you are passing the parameters in wrong sequence. The function is declared as x(m1, d1, y1, m2, d2, y2), but you are calling x(m1, m2, d1, d2, y1, y2). This was hard to find out. :)

Also, I've cleaned up the code based on your original code. Note that you need to use the variable "i" in the loop instead of "y1" or "m2"

For example,

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

This code has no meaning. You wanted to use daysInMonth(i, y2);

Here is my suggestion:

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

The result is:

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

Here is the entire code:

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;

 }
}
链接地址: http://www.djcxy.com/p/96118.html

上一篇: 生命的宇宙和一切

下一篇: 线程“main”中的异常java.util.NoSuchElementException错误