-5

how can search specific word from file. like if we have have a text file and in filem, text is arranged line by line like...

The Apple is sweet.

the color of car is red.

the sky is blue..

Now user want to search car or red... and input through text box so how can we search from file that match input text from file text and show reslut. it may be possible that input word is more than 1 time in document then reslut also show how many times it appear in file. so how can we implement this....

slybloty
  • 6,346
  • 6
  • 49
  • 70
user3271822
  • 33
  • 1
  • 5

2 Answers2

0

You have an example of reading a file line by line here :

https://stackoverflow.com/a/22074145/3315914

All you need is replace a few lines:

     public static ArrayList<String> searchInFile(String word) {


    ArrayList<String> result = new ArrayList<String>();
    FileReader fileReader = null;
    try {
        fileReader = new FileReader(new File("input.txt"));
    } catch (FileNotFoundException e1) {
        System.err.println("File input.txt not found!");
        e1.printStackTrace();
    }

    BufferedReader br = new BufferedReader(file);
    String line;
    try {
        while ((line = br.readLine()) != null) {
          if (line.contains(word)) {
              result.Add(word);
           }
        }

    } catch (IOException e) {
        System.err.println("Error when processing the file!");
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                System.err.println("Unexpected error");
                e.printStackTrace();
            }
        }

    }
    return result;
}
Community
  • 1
  • 1
rpax
  • 4,468
  • 7
  • 33
  • 57
  • Never share code, if someone has not added any code.... but make the questioner think by providing some hint. although, good to see your answer.. This will just check if the word is present or not. however if the person needs the exact occurrence location, use indexOf() instead of contains(). – Anand Chavan Mar 05 '14 at 19:41
  • @anandc uhm... ok. I'll edit my answer, only for correctness. I'll take your advice about sharing code. – rpax Mar 05 '14 at 19:43
  • @anandc Decrypting the question, _Now user want to search car or red... and input through text box so how can we search from file that match input text from file text and show **reslut**. it may be possible that input word is more than 1 time in document then reslut also show how many times it appear in file. so how can we implement this_ Maybe he is talking about counting words? – rpax Mar 05 '14 at 19:45
  • we make people think, instead of spoon feeding :) – Anand Chavan Mar 05 '14 at 19:46
  • good point !
    This is just a algorithm, required modifications can be made in order to serve the purpose.
    – Anand Chavan Mar 05 '14 at 19:50
  • @rapx it is usefull for me.. – user3271822 Mar 05 '14 at 20:26
  • @Downvoter care to comment? – rpax Mar 05 '14 at 20:43
  • @rapx using this can i search with in specfic line like in file i have 20 lines and i want to search from line 10 to 15 can this possible if yae so kindly help how can i do this.... – user3271822 Apr 03 '14 at 18:31
  • @user3271822 Keep a counter variable, and do nothing while is< 10 – rpax Apr 03 '14 at 18:34
  • @rapx one thing more that if a line contain searchword like line number 10 cotain search word then in while loop i want to serach anoter word from line number 5 to 15 another word how can i do this?? – user3271822 Apr 03 '14 at 18:48
  • @user3271822 I dont understand you – rpax Apr 03 '14 at 18:51
  • in this code we can search line by line so if a line number 10 contain searched word so in if condition i want to search another word from line number 5 to 15 how can do this?? @rpax – user3271822 Apr 03 '14 at 18:55
0

Here is a simple logic that one can apply

Load File
Iterate till the end
while iterating through every line, call lineString.indexOf("searchKeyword")
above line will let you know if the searchKeyword appears, if yes, you will get the starting location of the word in this string.

Anand Chavan
  • 4,338
  • 6
  • 23
  • 27