Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.task.task19.task1924;
- import java.io.*;
- import java.util.*;
- import java.util.regex.*;
- import java.util.stream.Collectors;
- public class Solution {
- public static Map<Integer, String> map = new HashMap<Integer, String>();
- static {
- map.put(0, "ноль");
- map.put(1, "один");
- map.put(2, "два");
- map.put(3, "три");
- map.put(4, "четыре");
- map.put(5, "пять");
- map.put(6, "шесть");
- map.put(7, "семь");
- map.put(8, "восемь");
- map.put(9, "девять");
- map.put(10, "десять");
- map.put(11, "одиннадцать");
- map.put(12, "двенадцать");
- }
- public static void main(String[] args) throws IOException {
- List<String> content = readFile();
- List<String> contentWithNumbersSpelled = replaceLines(content);
- printList(contentWithNumbersSpelled);
- }
- private static List<String> readFile() throws IOException {
- try (BufferedReader filenameReader = new BufferedReader(new InputStreamReader(System.in));
- BufferedReader fileReader = new BufferedReader(new FileReader(filenameReader.readLine()))) {
- return fileReader.lines().collect(Collectors.toList());
- }
- }
- private static List<String> replaceLines(List<String> content) {
- List<String> list = content.stream()
- .map(Solution::spellNumbers)
- .collect(Collectors.toList());
- return list;
- }
- private static String spellNumbers(String line) {
- Pattern pattern = Pattern.compile("\\b([0-9]|1[0-2])\\b");
- Matcher matcher = pattern.matcher(line);
- StringBuffer lineReplacer = new StringBuffer(line.length());
- while (matcher.find()){
- String toReplace = matcher.group();
- String replacement = map.get(Integer.valueOf(toReplace));
- matcher.appendReplacement(lineReplacer, replacement);
- }
- matcher.appendTail(lineReplacer);
- return String.valueOf(lineReplacer);
- }
- private static void printList(List<String> lines) {
- lines.forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment