0

I've written the code below and from what I can guess it should execute correctly but it doesn't and I'm hitting my head against a wall. What I'm trying to do after finding the objects to be added to the temp vector list is to loop through the vector list take each file name and count the number of occurrences of a word. I can get both pieces of code to work seperately but when I put them together the code stops executing after the temp vector list has been filled. Can anyone see from my code below what is stopping the rest of the code from executing?

EDITTED CODE >> I've changed a few things around in the code to format correctly and to remove the duplication but now nothing outputs to the screen. I'm sure it's something simple but I just don't seem to be able to see it.

                    for(int m = 0; m < temp.size() && occurCount < wordCount;m++)
                    {
                    File aFile2 = new File(temp.elementAt(m));
                    FileReader aFileReader = new FileReader(aFile2);
                    BufferedReader aBufferReader = new BufferedReader(aFileReader);

                    while((line = aBufferReader.readLine()) != null && occurCount < wordCount)
                    {

                        words  = line.toLowerCase().split(" ");
                        if (line == null)
                            break;
                        else 
                        {
                            for(int k = 0; k < words.length && occurCount < wordCount;k++)
                            {
                                if(words[k].matches(wordToSearch))
                                {
                                    occur++;

                                    q.add(words[k]);

                                }
                                else if(words[k].matches(pattern))
                                {
                                    System.out.println(words[k]);

                                        if(temp.contains(words[k]))
                                            System.out.println("Word already in list");
                                        else
                                        {
                                            temp.add(words[k]); 
                                        }

                                }


                            }
                        }
                        lineFile = "";  


                    }
                    occurCount = occur;
                    occur = 0;
                    aBufferReader.close();
                    searched.add(temp.elementAt(m));
                    occurCount2 += occurCount;
                    //occurCount += occurCount;



                   System.out.println(occurCount);
                   System.out.println(q);
                   System.out.println(temp);
                   System.out.println(searched);
3
  • 4
    This is a LOT of code. You should try to isolate the problem and attach only the relevant part. Commented Dec 1, 2012 at 10:32
  • It seems as if you are adding at least one String to the list, then for each string ion the list you are reading a file and adding all the words in that file to the list which would then for each new word in the list read the file again etc...That may simply mean that your program is taking a long time to execute. Commented Dec 1, 2012 at 10:40
  • I thought so too Vincent but it just exits out of the program and doesn't display anything to the console. I tried to rewrite it just using the buffer once but it gives me an infinite loop. That's why I seperated the code into two. Commented Dec 1, 2012 at 10:45

2 Answers 2

1
                char aChar = 'x';
                for (int i = 0; i <= line.length()-1; i++) 
                {
                    aChar = line.charAt(i);
                    lineFile += aChar;
                }

                words = lineFile.split(" ");
                if (line == null)
                    break;
                lineFile = "";  

would be the same as:

               words = line.split(" ");

                for(int k = 0; k <= words.length - 1;k++)
                {
                    if(words[k].matches(pattern))
                    {
                        System.out.println(words[k]);
                        if(!searched.contains(words[k]))
                        {
                            if(temp.contains(words[k]))
                                System.out.println("Word already in list");
                            else
                            {
                                temp.add(words[k]); 
                            }
                        }
                    }

one would normally use for (int i = 0; i < n; i++) instead of <= n - 1.

You used temp.add, which probably should have been searched.add, as otherwise the outer loop goes on and on: (for ... < temp.size())

                for (String word : words)
                {
                    if (word.matches(pattern))
                    {
                        System.out.println(word);
                        if (!searched.add(word))
                            System.out.println("Word already in list");
                        }
                    }

add returns false when the word was already contained in the set.

words has to be generic typed like Set<String>.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Joop, I've made a number of changes
0

First of all, refactor your code. I mean think about it and try to write it a little bit better. For example the following part

line = line.toLowerCase();
char aChar = 'x';
for (int i = 0; i <= line.length()-1; i++) {
    aChar = line.charAt(i);
    lineFile += aChar;
}

words  = lineFile.split(" ");

can be rewritten as

words = line.toLowerCase().split(" ");

And you have the same parts of code written twice - extract them to some methods.

I would guess there is much more of that in your code. When you have it, paste the relevant part here and we'll see...

1 Comment

Thanks Tomas, I have made a few changes to the code based on your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.