These are explanatory comments on the modifyLine method : public static String modifyLine(String line){ // the word variable stores each word in the line : the string produced until we meed a non-alphanumerical character StringBuilder word = new StringBuilder(); // the result variable stores the new version of the line StringBuilder result = new StringBuilder(); for(char c : line.toCharArray()){ // if the character is neither an alphabet or a digit if(! (Character.isAlphabetic(c) || Character.isDigit(c))){ // if the current word is numerical then check whether we should replace it or not if(isNumerical(word.toString())){ int i = Integer.parseInt(word.toString()); // if ( i>=0 && i<=12) if(map.containsKey(i)) result.append(map.get(i)); else result.append(word); } // if the current word is not numerical then we just need to append it to the result var else{ result.append(word); } // either way we need to append the non-alphanumerical character to the result var result.append(c); // reset the word var word = new StringBuilder(); } else{ //add the read character to the current word word.append(c); } } return result.toString(); }