Word Index map illegal escape character

51 views Asked by At

I don't understand what I'm doing wrong here.

The instructions are as follows:

In this project, you will read a text file. For every newly recognized word, it would be entered into an index and the line on which it occurred. For any word that was previously recognized, the line where it occurred would entered into the index.

Because such words are so common, any word containing three (3) or less characters are to be ignored. Similarly, punctuation and capitalization should also be ignored. Hyphenated words are treated as a single word without hyphenation. For example, ‘overheard’ and ‘overheard’ are the same.

Words may have multiple punctuation, either intentionally or by accident. Thus, ‘equal.’, ‘equal….’, ‘equal.,.,.’ and ‘.,.;equal’ are all the same word – ‘equal’. After the input has been exhausted, the index of words and their line positions should be printed in alphabetical order.

import java.util.*;
import java.io.*;

public class Proj5 {

public static void main(String[] args) throws FileNotFoundException {
    Map<String, ArrayList<Integer>> occurrence = new HashMap<>();
    Scanner scan = new Scanner(new FileReader("words.txt"));

    for (int lineNum = 1; scan.hasNextLine(); lineNum++) {
        String line = scan.nextLine();
        String[] words = line.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");

        for (String word : words) {
            if (word.length() >= 4) {
                word = word.toLowerCase();
                ArrayList<Integer> list = occurrence.get(word);

                if (list == null) {
                    list = new ArrayList<>();
                    occurrence.put(word, list);
                }
                if (!list.contains(lineNum)) {
                    list.add(lineNum);
                }
            }
        }
    }
    List<String> words = new ArrayList<>();
    words.addAll(occurrence.keySet());
    Collections.sort(words);

    words.stream().forEach((word) -> {
        System.out.println(word + " - " + occurrence.remove(word));
    });
  }
}

Yes i do have a words.txt in the directory.

0

There are 0 answers