0
I have the following methods in java, the split method separates each word from a text and stores it in a Treeset<> to maintain the alphabetical order and returns the Treeset<> in question. The compare method should take this Treeset<> and compare it to the text passed by parameter, taking each word, using the method. getLineNumber of the Linenumberreader class and store each line where that word appears put in a Map<> each word and the list where it appears.
public Collection<String> split(String book){
try{
FileReader path = new FileReader(book);
LineNumberReader read = new LineNumberReader(path);
String line;
while((line = read.readLine()) != null){
line = line.replaceAll("[^a-zA-Z]", " ").toLowerCase();
split = line.split(" ");
for(String s : split){
if(s.length() >= 1 && !palavras.contains(s)){
palavras.add(s);
}
}
}
path.close();
read.close();
}catch(FileNotFoundException e){
e.getStackTrace();
System.out.println("Caminho para o arquivo invalido!");
}catch(IOException ex){
ex.getStackTrace();
}
return palavras;
}
public Map<String, List<Integer>> compare(Collection<String> list, String book){
FileReader path;
try {
path = new FileReader(book);
LineNumberReader read = new LineNumberReader(path);
String line;
for(String s : list){
ArrayList<Integer> lineNro = new ArrayList<>();
while((line = read.readLine()) != null){
line = line.replaceAll("[^a-zA-Z]", " ").toLowerCase();
split = line.split(" ");
for(String a : split){
if(s.equals(a)){
lineNro.add(read.getLineNumber());
}
words.put(a, lineNro);
}
}
}
for(Map.Entry<String, List<Integer>> e : words.entrySet()){
System.out.println(e);
}
path.close();
read.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Spliter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Spliter.class.getName()).log(Level.SEVERE, null, ex);
}
return words;
}
The result is a list of words but all words receive the same list of Integer. The expected result It would be a list of words and for each word a list composed by the lines where that word appears.
What is wrong with your code? What is the result obtained and what is expected? If possible create a EMCV
– Mansueli
Edited, thank you for your attention.
– Lone_Dryad