In: Computer Science
in java (just a line of code will do)
Your program will read in the text file, word by word. You are to ignore leading and trailing punctuation and capitalization (i.e., "Castle", "castle?", and "CASTLE" are all equivalent to castle). Convert all words to lower case on input, or you can use case-insensitive comparisons - your choice. You will be putting the words into several implementations of linked lists and comparing the results. If, after trimming punctuation, a "word" consists of no letters, then discard the word. Such a case might arise if your word-reading strategy assumes that every word is delimited by spaces, in which case, a dash would constitute a "word", but would not contain any letters, so it would be discarded (for example, "comparisons - your choice" should be three words, though your space-delimited parser might identify "-" as a fourth "word", but the “-“ should be discarded).
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class FileWords {
public static void main(String[] args) throws
FileNotFoundException {
File file = new
File("D:\\java\\words.txt");
Scanner sc = new
Scanner(file);
List<String> words = new
LinkedList<>();
while (sc.hasNext()) {
String word =
sc.next();
word =
word.trim().replaceAll("[^A-Za-z0-9]", "");
if
(!word.isBlank()) {
// if(!words.contains(word))
words.add(word.toLowerCase());
}
}
System.out.println("Total " +
words.size() + " words found\nWords in the Text File:\n\n " +
words);
}
}