2016-07-04 5 views
1

私は2つのパターンがあり、それぞれが数値を単語に変換して値を返します。どのように価値を得て正しい位置に印刷するのですか?戻り値と正しい位置に印刷

public static String replaceIfNeeded(String value){ 

String patternDate = "\\d{1,2}/\\d{1,2}/\\d{4}"; // dd/mm/yyyy @ d/m/yyyy 
String patternTime = "\\d{1,2}:\\d{1,2}"; // hh:mm (time) 

Pattern date_pattern = Pattern.compile(patternDate); 
Matcher date_matcher = date_pattern.matcher(value); 

Pattern time_pattern = Pattern.compile(patternTime); 
Matcher time_matcher = time_pattern.matcher(value); 

if (date_matcher.find()) { // find date pattern 
    String get_date = date_matcher.group(); 
    String parts[] = get_date.split("/"); // split process 
    String get_day = parts[0]; //store day in first array 
    String get_month = parts[1]; //store month in second array 
    String get_year = parts[2]; //store year in third array 

    String s = NumberConvert.convert(Integer.parseInt(get_day)) + " of" + 
       NumberConvert.convert(Integer.parseInt(get_month)) + 
       NumberConvert.convert(Integer.parseInt(get_year)); 

    value = date_matcher.replaceFirst(s); //replace number to words in variable value 
} 

else if (time_matcher.find()) { // find time pattern 
    String get_time = time_matcher.group(); 
    String parts[] = get_time.split(":"); // split process 
    String get_hour = parts[0]; //store hour in first array 
    String get_minute = parts[1]; // store minute in second array 

    String s = NumberConvert.convert(Integer.parseInt(get_hour)) + " and " + 
       NumberConvert.convert(Integer.parseInt(get_minute)) + " minutes "; 

    value = time_matcher.replaceFirst(s); // replace number to words in variable value 
}  
    return value; // return value 

} 

主な方法はここにある:

私はこのreplaceIfNeeded()メソッドを作成したユーザーからの

public static void main(String[] args) { 

     Scanner reader = new Scanner(System.in); 
     System.out.println("Enter your text here: "); 
     String text = reader.nextLine(); 

     // remove any space between "/" 
     String data = text.replaceAll("\\s+/", "/").replaceAll("/\\s+", "/"); 
     String result = replaceIfNeeded(data); 
     System.out.println(result); 

入力:

日の私の誕生は12月12日/ 2003年3月23日23:12。

結果:

日の私の誕生は23 12分に2003年12月12日と時間です。

日付パターンの戻り値がありません。私を案内してください。私は価値を返し、正しい位置に印刷したいと思います。ユーザーは複数の日時を入力できます。何の日付パターンは(else文を削除)が見つからなかった場合

時間パターンのためにそれが唯一のチェック:

+0

ヒント:あなたの方法に少ないものを入れます。今のところ、あなたのコードはかなり複雑です。読むのがとても難しくなります。あなた自身と – GhostCat

+0

NumberConvertクラスは –

答えて

0

replaceIfNeeded方法は、()二つの問題があります。

2番目のマッチャーは、編集した値で初期化する必要があります。

は、この機能を試してみてください:

public static String replaceIfNeeded(String value){ 

    String patternDate = "\\d{1,2}/\\d{1,2}/\\d{4}"; // dd/mm/yyyy @ d/m/yyyy 
    String patternTime = "\\d{1,2}:\\d{1,2}"; // hh:mm (time) 

    Pattern date_pattern = Pattern.compile(patternDate); 
    Matcher date_matcher = date_pattern.matcher(value); 

    if (date_matcher.find()) { // find date pattern 
     String get_date = date_matcher.group(); 
     String parts[] = get_date.split("/"); // split process 
     String get_day = parts[0]; //store day in first array 
     String get_month = parts[1]; //store month in second array 
     String get_year = parts[2]; //store year in third array 

     String s = NumberConvert.convert(Integer.parseInt(get_day)) + " of" + 
       NumberConvert.convert(Integer.parseInt(get_month)) + 
       NumberConvert.convert(Integer.parseInt(get_year)); 

     value = date_matcher.replaceFirst(s); //replace number to words in variable value 
    } 

    // "else" removed and time_matcher initialized with the edited value 
    Pattern time_pattern = Pattern.compile(patternTime); 
    Matcher time_matcher = time_pattern.matcher(value); 
    if (time_matcher.find()) { // find time pattern 
     String get_time = time_matcher.group(); 
     String parts[] = get_time.split(":"); // split process 
     String get_hour = parts[0]; //store hour in first array 
     String get_minute = parts[1]; // store minute in second array 

     String s = NumberConvert.convert(Integer.parseInt(get_hour)) + " and " + 
       NumberConvert.convert(Integer.parseInt(get_minute)) + " minutes "; 

     value = time_matcher.replaceFirst(s); // replace number to words in variable value 
    }  
    return value; // return value 

} 
+0

と同じことが起こります – Paranrax

関連する問題