I have the following methods in java, the split method separates each word from a text and saves it in a TreeSet < > to keep alphabetical order and returns the TreeSet < > in question. The compare method should take this TreeSet < > and compare with the text passed by parameter, picking up each word, using the .getLineNumber method of the LineNumberReader class and saving each line where that word appears to 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 are given the same list as Integer. The expected result would be a list of words and for each word a list consisting of the lines where that word appears.