0

I want to find the line number of a text file by each word, however, the method I wrote below only gives the first number while I need a list of line numbers.

For instance, if "a" occurs in lines: 1,3,5, it should have a list of [1,3,5]. This list result then will be passed into another method for further process. But, my result only shows [1] for "a".

Can someone help me fix this? Thank you!

    public SomeObject<Word> buildIndex(String fileName, Comparator<Word> comparator) {
        SomeObject<Word> someObject = new SomeObject<>(comparator);

        Comparator<Word> comp = checkComparator(someObject.comparator());
        int num = 0;
        if (fileName != null) {
            File file = new File(fileName);
            try (Scanner scanner = new Scanner(file, "latin1")) {
                while (scanner.hasNextLine()) {
                    String lines;
                    if (comparator instanceof IgnoreCase) {
                        lines = scanner.nextLine().toLowerCase();
                    } else {
                        lines = scanner.nextLine();
                    }
                    if (lines != null) {
                        String[] lineFromText = lines.split("\n");

                        List<Integer> list = new ArrayList<>();
                        for (int i = 0; i < lineFromText.length; i++) {
                            String[] wordsFromText = lineFromText[i].split("\\W");
                            num++;

                            for (String s : wordsFromText) {

                                if (s != null && lineFromText[i].contains(s)) {
                                    list.add(num);
                                }

                                if (s != null && !s.trim().isEmpty() && s.matches("^[a-zA-Z]*$")) {
                                    doInsert(s, comp, someObject, list);
                                }
                            }


                        }

                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return someObject;
    }
Andrea
  • 23
  • 3
  • [The answer](https://stackoverflow.com/questions/7548519/finding-line-number-of-a-word-in-a-text-file-using-java/7548553) to this question might give the answer you're looking for. – Johnathan Davidow Dec 10 '19 at 17:11
  • I think the solution doesn't work in my case. – Andrea Dec 10 '19 at 17:29
  • You are doing many things I don't understand. Why are you trying to split a line on "\n" when Scanner reads in one line at a time? And the `Comparator Instanceof IgnoreCase` doesn't make sense . Why not just ignore case and be done with it? – WJS Dec 10 '19 at 18:21

1 Answers1

0

Does something like this work for you?

  1. It reads in the lines one at a time.
  2. Finds the words by splitting on spaces.
  3. Then puts the words and the line numbers in a map where the key is the word an the value is a list of line numbers.
      int lineCount = 1;
      String fileName = "SomeFileName";
      Map<String, List<Integer>> index = new HashMap<>();
      Scanner scanner = new Scanner("fileName");

      while (scanner.hasNextLine()) {
         //get single line from file
         String line = scanner.nextLine().toLowerCase();
         //split into words
         for (String word : line.split("\\s+")) {
             // add to lineNumber to map if List already there.
             // otherwise add new List and then add lineNumber  
             index.compute(word,
                   (wd, list) -> list == null ? new ArrayList<>()
                        : list).add(lineCount);
         }
         // bump lineCount for next line
         lineCount++;
      }

Print them out.

      index.forEach((k, v) -> System.out.println(k + " --> " + v));

WJS
  • 36,363
  • 4
  • 24
  • 39