Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Map words to line number in text file

    Hi everyone,

    I'm working on a small project and have run into some problems.
    Briefly described, what I'm attempting to do is:

    I've a rather large text file with a different sentence and I need to find all the words in each sentence and add them to some sort of index, so that the structure is: key = word and value = line number(s). For example like this:

    mistake | 0, 2, 4, 6
    sun | 0, 1, 5, 10
    rain | 3, 4, 10, 22

    I need to make a tool to find the occurrence of a word in a file, so if I search for the word "drum" is say: Drum occur 5 times in line 3, 8, 9, 11, 32

    I've played around with some different structures but unfortunately I haven't been successful.

    I read my text file as such:
    Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        while (true) {
        String str = br.readLine();
        if (str == null) break;
        lines.add(str);
        }
    Any help would be appreciated.

    Sincere
    - Mestika

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    Utah
    Posts
    30
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Look into HashMaps or Matrixes


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •