Что то я не совсем понимаю Мистера Валидатора!
package com.javarush.task.task19.task1924;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Замена чисел
*/
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) {
BufferedReader inputPath = new BufferedReader(new InputStreamReader(System.in));
String stringPath = "";
try{
stringPath = inputPath.readLine();
} catch (IOException e){
e.printStackTrace();
}
ArrayList<String> oldList = readFile(stringPath);
ArrayList<String> resultList = new ArrayList<>();
for (int i = 0; i < oldList.size(); i++ ){
String strings = oldList.get(i);
for (int b = 1; b <= strings.length(); b++){
resultList.add(strings.substring(b-1,b));
}
strings = null;
}
String stForNumber = "";
for (int i = 1; i < resultList.size(); i++){
try{
Integer.parseInt(resultList.get(i-1));
stForNumber += resultList.get(i-1);
} catch (NumberFormatException e){
if (i > 2){
for(Map.Entry<Integer,String> cell : map.entrySet()){
if (stForNumber.length() == 1){
if (stForNumber.equals(String.valueOf(cell.getKey())) & resultList.get(i-3).equals(" ") |
stForNumber.equals(String.valueOf(cell.getKey())) & resultList.get(i-3).isEmpty()){
System.out.print(cell.getValue());
stForNumber = "";
}
} else if (stForNumber.length() == 2){
if (stForNumber.equals(String.valueOf(cell.getKey()))){
System.out.print(cell.getValue());
stForNumber = "";
}
}
}
}
System.out.print(stForNumber+resultList.get(i-1));
stForNumber = "";
}
}
try{
inputPath.close();
} catch (IOException NOP){}
}
private static ArrayList<String> readFile(String string){
BufferedReader fileReader = null;
try{
fileReader = new BufferedReader(new FileReader(string));
} catch (IOException e){
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<>();
try{
while (fileReader.ready()){
list.add(fileReader.readLine()+"\n");
}
fileReader.close();
} catch (IOException e){
e.printStackTrace();
}
return list;
}
}
/*
boolean backB = false;
boolean centreB = false;
boolean nextB = false;
String searchSpace = resultList.get(0);
String allST = "";
for (int i = 0; i < resultList.size(); i++){
String back = null;
String centre = null;
String next = null;
try{
// Парсим лист Ищем число
Integer.parseInt(resultList.get(i));
centre = resultList.get(i);
centreB = true;
if (i > 0){
if (i > 2){
searchSpace = resultList.get(i-2);
}
try{
Integer.parseInt(resultList.get(i-1));
back = resultList.get(i-1);
backB = true;
} catch (NumberFormatException e){
backB = false;
}
}
if (i+1 <= resultList.size()){
try{
Integer.parseInt(resultList.get(i+1));
next = resultList.get(i+1);
nextB = true;
} catch (NumberFormatException e){
nextB = false;
}
}
} catch (NumberFormatException e){
System.out.print(resultList.get(i));
allST += resultList.get(i);
centreB = false;
}
if (centreB == true & backB == false & nextB == false){
int c = Integer.parseInt(centre);
for (Map.Entry<Integer,String> cell : map.entrySet()){
if (c == cell.getKey()){
System.out.print(cell.getValue());
allST += cell.getValue();
}
}
centreB = false;
}
if (centreB == true & backB == true & nextB == false){
String numberSt = back+""+centre;
int numb = Integer.parseInt(numberSt);
for (Map.Entry<Integer,String> cell : map.entrySet()){
if (numb == cell.getKey()){
System.out.print(cell.getValue());
allST += cell.getValue();
}
}
centreB = false;
backB = false;
}
}
*/
/*
for (int i = 0; i < oldList.size(); i++ ){
String strings = oldList.get(i);
int number = Integer.MAX_VALUE;
for (int b = 1; b < strings.length(); b++){
try {
Integer.parseInt(strings.substring(b-1,b));
try {
if (b+1 <= strings.length()){
Integer.parseInt(strings.substring(b-1,b+1));
try{
if (b >= 2){
Integer.parseInt(strings.substring(b-2,b-1));
}
} catch (NumberFormatException e){
}
} else {
number = Integer.parseInt(strings.substring(b-1,b));
resultList.add(String.valueOf(number));
}
} catch (NumberFormatException NOP){
number = Integer.parseInt(strings.substring(b-1,b));
resultList.add(String.valueOf(number));
}
} catch (NumberFormatException e){
resultList.add(strings.substring(b-1,b));
}
}
strings = null;
}
*/