Hello!

I am facing a problem over and would like to ask if anyone knows the solution.
On the following code i am looping files for sentences and then words. If a specific word is found i would like to store a String with the specific phrase.
In example word[x-3] until word[x+3]

any Suggestions ?
Thanks in advance.

public void ScanSearch() {

        for (int i = 0; i < files.length; i++) {
            try {
                
            	BufferedReader fileIn = new BufferedReader(new FileReader(files[i][1]));
            	int lineCount =1;
            	String line = fileIn.readLine();
            	while(line != null){
            		String[] lineWords = line.split(" ");
            		for (int j=0; j<lineWords.length; j++){
            			System.out.println(searchType);
            			System.out.println(lineWords[j]);

            			if (lineWords[j] == inputArr[0]){
                			System.out.println("bingo");
            			}
            			
            			
            		}
            		lineCount++;
            		line = fileIn.readLine();
            	}
            	
            } catch (Exception e) {
               e.printStackTrace();
            }

        }

So if you're reading this post, for example, and you're looking for the word "example", you'd have two strings, "this post, for example, and you're looking " and "for the word "example", you'd have two" - is that right?
[EDIT: okay, more than two strings, but let's not get recursive... :) ]

If you want to make a new class, I'd make a variant on a queue. For some length n, it accepts the first n tokens, then once full it loses a token for each token pushed, dropping the tokens on a FIFO basis of course. It would then have a method to return all n tokens, and you're pretty much done - when you get a match, you build a string with the n tokens, plus the match token, plus the next n tokens in the input.

Or, simpler maybe, you read the string and split it into an array of words. Read each word: if it's the match word, you can just grab the array elements surrounding it. Depends on your requirements, I guess - the array is easier if you have a static file, the other might be easier if you're reading input from a stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.