1

我需要创建一个程序,从文件 dates.txt 接收日期并将它们输出到 dates.out,如果日期有效,那么它将提供新格式和日期在一年中的日期,如果不是将返回“无效日期:(原始日期和格式)”似乎我已经在下面完成了所有操作,但是当我输入正确的文件名时,我不断收到下面的错误,我不确定问题是什么,因为我要求一个有效的 int。查看普通版?注意:代码块中的文本内容会自动换行

Input file name: dates.txt  
Exception in thread "main" java.util.InputMismatchException  
    at java.util.Scanner.throwFor(Scanner.java:909)  
    at java.util.Scanner.next(Scanner.java:1530)  
    at java.util.Scanner.nextInt(Scanner.java:2160)  
    at java.util.Scanner.nextInt(Scanner.java:2119)  
    at Lab12.main(Lab12.java:43)  

查看普通版?注意:代码块中的文本内容会自动换行

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

public class Lab12{  
   public static final int JAN = 1;  
   public static final int FEB = 2;  
   public static final int MAR = 3;  
   public static final int APR = 4;  
   public static final int MAY = 5;  
   public static final int JUN = 6;  
   public static final int JUL = 7;  
   public static final int AUG = 8;  
   public static final int SEP = 9;  
   public static final int OCT = 10;  
   public static final int NOV = 11;  
   public static final int DEC = 12;  

    public static void main(String[] arg)  
                        throws FileNotFoundException{  
        Scanner console = new Scanner(System.in);  
        Scanner input = getInput(console);  



        while(input.hasNextLine()){  

            String text = input.nextLine();  
            Scanner time = new Scanner(text);  

            int day = time.nextInt(); // <<<< line 43  
            int month = time.nextInt();   
            int year = time.nextInt();  
            if(validDate(day, month, year)){  
               output.print (formatDate(day, month, year));  
               }  
            else  
               output.print ("Invalid date: "+day+"/"+month+"/"+year);  
        }  
    }  

    public static Scanner getInput(Scanner console)  
                        throws FileNotFoundException{  
        System.out.print ("Input file name: ");  
        File f = new File(console.nextLine());  
        while(!f.canRead()){  
            System.out.println ("File not found. Try again.");  
            System.out.print ("Input file name: ");  
            f = new File(console.nextLine());  
        }  
        return new Scanner(f);  
    }  

    public static boolean isLeapYear(int year){  
        boolean isLeapYear = false;  
        if(year % 400 == 0)  
            isLeapYear = true;  
        else if(year % 4 == 0 && year % 100 == 0)  
            isLeapYear = false;  
        else if(year % 4 == 0)  
            isLeapYear = true;  
        else  
            isLeapYear = false;  
        return isLeapYear;  
    }  
    //http://www.java-forums.org/new-java/41020-day-number-count.html  
    public static int dayNumber(int day, int month, int year){  
      int daysInMonth = 0;  
      int days = 0;  
      boolean leapYear = isLeapYear(year);  
        for(int i = 1; i < month; i++){  
            switch(month){  
                case 1: daysInMonth += 31;  
                break;  
                case 2: if(leapYear)  
                     daysInMonth = 29;  
                    else  
                     daysInMonth = 28;  
                break;  
                case 3: daysInMonth += 31;  
                break;  
                case 4: daysInMonth += 30;  
                break;  
                case 5: daysInMonth += 31;  
                break;  
                case 6: daysInMonth += 30;  
                break;  
                case 7: daysInMonth += 31;  
                break;  
                case 8: daysInMonth += 31;  
                break;  
                case 9: daysInMonth += 30;  
                break;  
                case 10: daysInMonth += 31;  
                break;  
                case 11: daysInMonth += 30;  
                break;  
                case 12: daysInMonth += 31;  
                break;  
            default:  
            break;  
         }  
         while(month <= 12){  
            days += days + daysInMonth + day;  
            month++;  
         }  
        }  
        return days;  
    }  

    public static boolean validDate(int day, int month, int year){  
        boolean validDate = false;  
        if(year >= 1 && year <= 3000){  
            if(month >= 1 && month <= 12){  
                if(month == 4 || month == 6 || month == 9 || month == 11   
                                    && day >= 1 && day <= 30){  
                    validDate = true;  
                }else if(month == 1 || month == 3 || month == 5 || month == 7  
                                    || month == 8 || month == 10 || month == 12 &&  
                                    day <=31){  
                    validDate = true;  
                }else if(month == 2){  
                    boolean leapYear = isLeapYear(year);  
                    if(leapYear = true && day >= 28){  
                        validDate = false;  
                    }  
                    else if(leapYear = false && day <= 29){  
                        validDate = true;  
                    }  
                    else{  
                        validDate = true;  
                    }  
                }  
            }  
            else  
                validDate = false;  
        }  
        else  
            validDate = false;  
        return validDate;  
    }  

    public static String formatDate(int day, int month, int year){  
      String formatDate = String.format ("%d-Jan-%d", day, year);  
      return formatDate;  
    }  
}

dates.txt 文件的内容

10/1/1999
12/31/2000
2/29/1900
2/1/1996
1/1/2097
2/29/2000
7/4/1776
5/32/3001
0/2/1234
8/0/2345
9/30/3001
2/29/2010
3/31/2001
13/3/1867
12/31/3000 
4

1 回答 1

0

您需要将输入作为字符串

“2/4/05”他们解析它

 String dateString = input.nextLine();

 String[] tokens = dateString.split("/");

 int month = Integer.parseInt(tokens[0]);
 int day = Integer.parseInt(tokens[1]);
 int year = Integer.parseInt(tokens[2])

/由于日期中的 ', 您会收到错误消息。

于 2013-10-31T01:39:35.980 回答