This should be filtering our words that contain the integer 0-9. Also any punctuation in a word is stripped before storage. After the constructor returns, the new instance will have all the information it needs to do analysis; the file will not be needed again. ... But it's not filtering at all.
A sample text file is: the quick brown fox jumped over the lazy dog.
Shortest words: [the, dog, fox]
but mine returns: [the, fox]
as you can see, it doesn't add into dog. because of the period.
public class TextProcessorImpl implements TextProcessor {
private String filename;
private ArrayList<String> lst;
public TextProcessorImpl(String filename) {
this.filename = filename;
lst = new ArrayList<String>();
String current;
Scanner scan = TextReader.openFile(filename);
//ArrayList<String> lst = new ArrayList<String>();
while (scan.hasNext()) {
current = scan.next();
if (current.matches(".*[0-9].*")) {
}
else {
current = current.replaceAll("\\p{Punct}+", "");
if (current.isEmpty()) {
}
else {
lst.add(current);
}
}
}
}
public Collection<String> getShortestWords() {
String shortestWord = null;
String current;
Scanner scan = TextReader.openFile(filename);
ArrayList<String> lst = new ArrayList<String>();
while (scan.hasNext()) { //while there is a next word in the text
current = scan.next(); //set current to the next word in the text
if (shortestWord == null) { //if shortestWord is null
shortestWord = current; //set shortestWord to current
lst.add(shortestWord); //add the shortest word to the array
}
if (current.length() < shortestWord.length()) { //if the current word length is less than previous shortest word
shortestWord = current; //set shortest word to the current
lst.clear(); //clear the previous array
lst.add(shortestWord); //add the new shortest word
}
else if(current.length() == shortestWord.length()){ //if the current word is the same length as the previous shortest word
if(!lst.contains(current))
lst.add(current);
}
}
return lst;
}
Aucun commentaire:
Enregistrer un commentaire