2012-12-07 28 views
1

我有一個Java編程任務,你必須在一行上輸入一個日期,它會給你一個基於日期的命理(星座圖)報告。假定用戶將輸入格式化日期,並用空格分隔。查找字符串中的下一個字符?

我可以通過使用in.nextInt()檢索輸入的月份,日期和年份。但是,我還必須檢查用戶是否爲日期的每個部分使用了正確的分隔字符,這意味着我只需檢查用戶是否使用了正斜槓。

查看我的代碼時,我目前使用charAt()來查找分隔字符。問題是日期不會總是14個字符。因此,10/17/2004年的日期長度爲14個字符,但1992年4月7日的日期只有12個字符長,這意味着「slash1」並不總是in.charAt(3),in後一種情況會出現在.charAt(2)中。

java是否有一個方法,允許像in.nextChar()?我知道它沒有,但我怎麼才能找到日期中的下一個字符?

編輯:我忘了最初反映這個,但我的教授說,我們不允許使用String.split()方法,出於某種原因。事情是,我得到的這個月,一天,一年都很完美。我只需要檢查該人是否使用正斜槓來分隔日期。如果輸入短劃線,則日期無效。

public void getDate() 
{ 
    char slash1, slash2; 

    do 
    { 
     System.out.print("Please enter your birth date (mm/dd/yyyy): "); 
     Scanner in = new Scanner(System.in); 
     String date = in.nextLine(); 

     month = in.nextInt(); 
     day = in.nextInt(); 
     year = in.nextInt(); 

     slash1 = date.charAt(3); 
     slash2 = date.charAt(8); 
    } while (validDate(slash1, slash2) == false); 

    calcNum(); 
} 

回答

0

我會使用掃描儀只是爲了得到一條線。再拆()上的空白行,並檢查字段:

import java.util.Scanner; 
import java.util.regex.Pattern; 

public class GetDate { 

    int month, day, year; 

    public static void main(String[] args) 
    { 
     GetDate theApp = new GetDate(); 
     theApp.getDate(); 

    } 

    public void getDate() 
    { 
     String date; 
     do 
     { 
     System.out.print("Please enter your birth date (mm/dd/yyyy): "); 
     Scanner in = new Scanner(System.in); 
     date = in.nextLine(); 
     } while (validDate(date) == false); 


     calcNum(); 
    } 

    boolean validDate(String date) 
    { 
     // split the string based on white space 
     String [] fields = date.split("\\s"); 

     // must have five fields 
     if (fields.length != 5) 
     { 
     return false; 
     } 

     // must have '/' separators 
     if (! (fields[1].equals("/") && fields[3].equals("/"))) 
     return false; 

     // must have integer strings 
     if (! (Pattern.matches("^\\d*$", fields[0]) && 
       Pattern.matches("^\\d*$", fields[2]) && 
       Pattern.matches("^\\d*$", fields[4]))) 
     return false; 

     // data was good, convert strings to integer 
     // should also check for integer within range at this point 
     month = Integer.parseInt(fields[0]); 
     day = Integer.parseInt(fields[2]); 
     year = Integer.parseInt(fields[4]); 

     return true; 
    } 

    void calcNum() {} 
} 
+0

對不起,我應該提到我的問題這點。這正是我所需要的,但我的老師說出於某種原因我們不允許使用String.split()方法。感謝您的意見。 – homersimpson

+0

擰它,我使用String.split()。謝謝! – homersimpson

+0

@homersimpson我添加了另一個不使用拆分的答案。 – Scooter

1

你可以考慮用"/"拆分輸入日期字符串,那麼你得到的字符串數組。下一步是將該數組中的每個字符串轉換爲int。

0

與其考慮使用什麼字符作爲分隔符,則關注所需的內容,即數字。

此代碼分割上數字,也不要緊多少數字是每個組中還是什麼字符用作分隔符:

String[] parts = input.split("\\D+"); 

它也幾乎沒有任何代碼,所以還有很多減少錯誤的機會。

既然您已經在String []中有數字部分,那麼您可以繼續進行計算。

下面是一些代碼,你可以使用下面的上述拆分:

if (parts.length != 3) { 
    // bad input 
} 

// assuming date entered in standard format of dd/mm/yyyy 
// and not in retarded American format, but it's up to you 
int day = Integer.parseInt(parts[0]; 
int month = Integer.parseInt(parts[1]; 
int year = Integer.parseInt(parts[2]; 
0

向前看流中,以確保它包含了你所期望的。

private static final Pattern SLASH = Pattern.compile("\\s*/\\s*"); 

static SomeTypeYouMadeToHoldCalendarDate getDate() { 
    while (true) { /* Might want to give user a way to quit. */ 
    String line = 
     System.console().readLine("Please enter your birth date (mm/dd/yyyy): "); 
    Scanner in = new Scanner(line); 
    if (!in.hasNextInt()) 
     continue; 
    int month = in.nextInt(); 
    if (!in.hasNext(SLASH) 
     continue; 
    in.next(SLASH); 
    ... 
    if (!validDate(month, day, year)) 
     continue; 
    return new SomeTypeYouMadeToHoldCalendarDate(month, day, year); 
    } 
} 
0

這使用掃描儀的方法來分析:

import java.util.Scanner; 
import java.util.InputMismatchException; 

public class TestScanner { 

    int month, day, year; 
    public static void main(String[] args) 
    { 
     TestScanner theApp = new TestScanner(); 
     theApp.getDate(); 
     theApp.calcNum(); 
    } 

    public void getDate() 
    { 
     int fields = 0; 
     String delim1 = ""; 
     String delim2 = ""; 
     Scanner in = new Scanner(System.in); 

     do 
     { 
     fields = 0; 
     System.out.print("Please enter your birth date (mm/dd/yyyy): "); 
     while (fields < 5 && in.hasNext()) 
     { 
      try { 
       fields++; 
       switch (fields) 
       { 
        case 1: 
        month = in.nextInt(); 
        break; 
        case 3: 
        day = in.nextInt(); 
        break; 
        case 5: 
        year = in.nextInt(); 
        break; 
        case 2: 
        delim1 = in.next(); 
        break; 
        case 4: 
        delim2 = in.next(); 
        break; 
       } 
      } 
      catch (InputMismatchException e) 
      { 
       System.out.println("ERROR: Field " + fields + " must be an integer"); 
       String temp = in.nextLine(); 
       fields = 6; 
       break; 
      } 
     } 
     } while (fields != 5 || validDate(delim1, delim2) == false); 
     in.close(); 
     System.out.println("Input date: " + month + "/" + day + "/" + year); 
    } 

    boolean validDate(String delim1, String delim2) 
    { 
     if ((! delim1.equals("/")) || (! delim2.equals("/"))) 
     { 
     System.out.println("ERROR: use '/' as the date delimiter"); 
     return false; 
     } 
     if (month < 1 || month > 12) 
     { 
     System.out.println("Invalid month value: " + month); 
     return false; 
     } 
     if ( day < 1 || day > 31) 
     { 
     System.out.println("Invalid day value: " + day); 
     return false; 
     } 
     if ( year < 1 || year > 3000) 
     { 
     System.out.println("Invalid year: " + year); 
     return false; 
     } 
     return true; 
    } 

    void calcNum() 
    { 

    } 

} 
相關問題