00vk

task1924/Solution

Jul 23rd, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package com.javarush.task.task19.task1924;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.regex.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Solution {
  9.  
  10.     public static Map<Integer, String> map = new HashMap<Integer, String>();
  11.  
  12.     static {
  13.         map.put(0, "ноль");
  14.         map.put(1, "один");
  15.         map.put(2, "два");
  16.         map.put(3, "три");
  17.         map.put(4, "четыре");
  18.         map.put(5, "пять");
  19.         map.put(6, "шесть");
  20.         map.put(7, "семь");
  21.         map.put(8, "восемь");
  22.         map.put(9, "девять");
  23.         map.put(10, "десять");
  24.         map.put(11, "одиннадцать");
  25.         map.put(12, "двенадцать");
  26.     }
  27.  
  28.     public static void main(String[] args) throws IOException {
  29.         List<String> content = readFile();
  30.         List<String> contentWithNumbersSpelled = replaceLines(content);
  31.         printList(contentWithNumbersSpelled);
  32.     }
  33.  
  34.     private static List<String> readFile() throws IOException {
  35.         try (BufferedReader filenameReader = new BufferedReader(new InputStreamReader(System.in));
  36.              BufferedReader fileReader = new BufferedReader(new FileReader(filenameReader.readLine()))) {
  37.             return fileReader.lines().collect(Collectors.toList());
  38.         }
  39.     }
  40.  
  41.     private static List<String> replaceLines(List<String> content) {
  42.         List<String> list = content.stream()
  43.                                    .map(Solution::spellNumbers)
  44.                                    .collect(Collectors.toList());
  45.         return list;
  46.     }
  47.  
  48.     private static String spellNumbers(String line) {
  49.         Pattern pattern = Pattern.compile("\\b([0-9]|1[0-2])\\b");
  50.         Matcher matcher = pattern.matcher(line);
  51.         StringBuffer lineReplacer = new StringBuffer(line.length());
  52.         while (matcher.find()){
  53.             String toReplace = matcher.group();
  54.             String replacement = map.get(Integer.valueOf(toReplace));
  55.             matcher.appendReplacement(lineReplacer, replacement);
  56.         }
  57.         matcher.appendTail(lineReplacer);
  58.         return String.valueOf(lineReplacer);
  59.     }
  60.  
  61.     private static void printList(List<String> lines) {
  62.         lines.forEach(System.out::println);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment