私は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文を削除)が見つからなかった場合
時間パターンのためにそれが唯一のチェック:
ヒント:あなたの方法に少ないものを入れます。今のところ、あなたのコードはかなり複雑です。読むのがとても難しくなります。あなた自身と – GhostCat
NumberConvertクラスは –