2013-10-31 59 views
1

我需要創建一個程序,它接收來自文件dates.txt的日期並將它們輸出到dates.out,如果日期有效,那麼它將提供一個新的格式和日期在一年中的日期,如果不是,它會返回「無效的日期:(原始日期和格式)」似乎我已經完成了下面,但當我輸入正確的時候,我仍然收到以下錯誤文件名,我不知道是什麼問題,因爲我打電話給一個有效的整數。 view plainprint? 注意:在碼塊文本內容是自動字包裹無法在string.hasNextLine中使用string.nextInt循環

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) 

視圖plainprint? 注:在代碼塊文本內容自動爲自動換行dates.txt文件的

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

內容

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 
+0

您還沒有選中hasNextInt()條件。而且,實際上「10/1/1999」不是一個有效的整數。 –

+0

太棒了!謝謝,我是一名新生,所以我忘記了這樣的簡單事情,我假設hasNextInt()已經應用 –

回答

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]) 

你得到的錯誤,因爲/的日期。

+0

...或通過調用['useDelimeter'方法](http:/ /更改默認的空格分隔符。 'Scanner'類的/docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.util.regex.Pattern))。 –