I have no idea what am I doing wrong. The output is exactly the same as in the example when I test the code. I had to miss something… I tried without the try, catch statements in the main method, but it still doesn't verify. I also tried without the method correctLine(String line) and put all the code into the main method, but the result is the same...
package com.codegym.task.task19.task1924;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/*
Replacing numbers
*/
public class Solution {
public static Map<Integer, String> map = new HashMap<Integer, String>();
static {
String[] list = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"};
for (int i = 0; i < list.length; i++) {
map.put(i, list[i]);
}
}
public static void main(String[] args) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.ready()) {
String line = reader.readLine();
correctLine(line);
}
bufferedReader.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void correctLine(String line) {
String[] list = line.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.length; i++) {
String y = list[i].replaceAll("[a-zA-Z0-9]", "");
list[i] = list[i].replaceAll("[^a-zA-Z0-9]", "");
try {
int x = Integer.parseInt(list[i]);
if (x >= 0 && x <= 12) {
list[i] = map.get(x);
}
} catch (Exception e) { }
builder.append(list[i]);
builder.append(y);
builder.append(" ");
}
System.out.println(builder.toString().trim());
}
}