The class is able to take in a text file, but i need a method that puts the word
ID: 3644407 • Letter: T
Question
The class is able to take in a text file, but i need a method that puts the words into a linked list and keeps track of the line the words are on and how many times the word appears. Uppercase and lowercase don't matter. It should look like this:Text Concordance
See Spot run. bite 1 [3]
See Jane run. jane 2 [2, 4]
See Spot bite. run 4 [1, 2, 4]
Run Jane, run! see 3 [1, 2, 3]
spot 2 [1, 3]
I can't figure out how to keep track of line numbers and how many times the words appears. I need help writing those methods. I have been able to take in the text file and disregard punctuation and to put everything to lower case, but I need help with the other methods.
Explanation / Answer
import java.util.*; import java.io.*; public class Concordance { static class AlphabeticalOrder implements Comparator { // Represents a Comparator that can be used for // comparing Strings according to alphabetical // order. It is an error to apply this // Comparator to objects that are non-strings. public int compare(Object obj1, Object obj2) { String str1 = (String)obj1; // Type-cast object to Strings. String str2 = (String)obj2; str1 = str1.toLowerCase(); // Convert to lower case. str2 = str2.toLowerCase(); return str1.compareTo(str2); // Compare lower-case Strings. } } static TextReader in; // An input stream for reading the input file. static PrintWriter out; // Output stream for writing the output file. static TreeMap index = new TreeMap(new AlphabeticalOrder()); // This TreeMap holds the concordance. Words from the file // are used as keys in the map. The value associated with // each word is a set that contains the line numbers on which // the word occurs in the file. The set contains values // belonging to the wrapper class, Integer. public static void main(String[] args) { openFiles(args); // Open input and output files. int lineNum = 1; // The number of the line in the input // file that is currently begin processed. while (true) { while (in.peek() != '' && ! Character.isLetter(in.peek())) { // Skip over non-letter characters, stopping when // end-of-file ('') or a letter is seen. If the // character is an end-of-line character, add one // to lineNum to reflect that fact that we are moving // on to the next line in the file. char ch = in.getAnyChar(); if (ch == ' ') { lineNum++; // Start a new line. } } if (in.eof()) { // The end-of-file has been reached, so exit from the loop. break; } String word = in.getAlpha(); // The next word from the file. if (word.length() > 2 && !word.equalsIgnoreCase("the")) { // Add the reference to word to the concordance, unless // the word is "the" or the word has lengthRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.