Greetings friends,
After an ungodly number of hours of figuring out how to break it into Strings without regex (thanks Breakiterator!) and then figuring out how to not lose the period at the end, it's not taking my answer even though it matches the output. It's not obvious to me what circumstances I'm missing that are making my code appear to produce the correct results, but in actuality not.
Thanks in advance,
Mike
package com.codegym.task.task19.task1924;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
import java.util.*;
/*
Replacing numbers
1. In a static block, initialize map with (number, word) pairs from 0 to 12 inclusive.
For example, (0, "zero"), (1, "one"), (2, "two")
2. Read the file name from the console, and then read the contents of the file.
3. Use map to replace all of the numbers with words.
4. Display the result.
5. Close the streams.
Example file data:
This costs 1 dollar, but this is 12.
The variable is called file1.
110 is a number.
Example console output:
This costs one dollar, but this is twelve.
The variable is called file1.
110 is a number.
Requirements:
1. The Solution class must have a public static Map<Integer, String> field called words that is initialized immediately.
2. The program must read the file name from the console (use BufferedReader).
3. The BufferedReader used for reading input from the console must be closed after use.
4. The program must read the file's contents (use FileReader).
5. The file input stream (FileReader) must be closed.
6. The program should write all of the lines from the file to the console, but any numbers must be replaced with the corresponding words in map.
7. The Solution class must have a static block that adds thirteen pairs to map.
*/
public class Solution {
public static Map<Integer, String> map = new HashMap<>();
static {
map.put(0, "zero");
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
map.put(9, "nine");
map.put(10, "ten");
map.put(11, "eleven");
map.put(12, "twelve");
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
while (fileReader.ready()) {
String line = fileReader.readLine();
//Now, you should have a list of strings (that look like integers) to replace.
ArrayList<String> lineStrings = numberFinder(line);
ArrayList<Integer> lineNumbers = new ArrayList<>();
for(String member : lineStrings){
lineNumbers.add(Integer.parseInt(member));
}
Collections.sort(lineNumbers);
Collections.reverse(lineNumbers);
lineStrings = new ArrayList<>();
for(Integer member : lineNumbers){
lineStrings.add(member + "");
}
for (String member : lineStrings) {
//System.out.println(member);
//If line contains a member (which it does since you've already identified it.
if (line.contains(member)) {
//The issue right now is that it's replacing a single digit rather than the whole number.
int integer = Integer.parseInt(member);
for (Map.Entry<Integer, String> entry : map.entrySet()) {
if (integer == entry.getKey()) {
line = line.replace(member, entry.getValue());
}
}
}
}
System.out.println(line);
}
fileReader.close();
}
//This method just collects the strings that need to be replaced.
public static ArrayList<String> numberFinder(String s) {
Locale currentLocale = Locale.US;
ArrayList<String> lineArrayList = new ArrayList<>();
ArrayList<String> lineIntegerArrayList = new ArrayList<>();
BreakIterator wordIterator = BreakIterator.getWordInstance(currentLocale);
wordIterator.setText(s);
int start = wordIterator.first();
int end = wordIterator.next();
while(end != BreakIterator.DONE){
String word = s.substring(start, end);
if(Character.isLetterOrDigit(word.charAt(0))){
lineArrayList.add(word);
}
start = end;
end = wordIterator.next();
}
//So, now, we should have an arrayList of the numbers that need replacing. Use these numbers to replace their String versions.
for(String member : lineArrayList){
try{
int integer = Integer.parseInt(member);
lineIntegerArrayList.add(integer + "");
}catch(NumberFormatException e){
e.getMessage();
}
}
return lineIntegerArrayList;
}
}