JHan
JHan

Reputation: 13

counting number of words in linked list

I'm trying to put a textfile into a linkedlist. I have successfully put the textfile into a linkedlist and I'm trying to count the number of specific word. My approach was to loop through the linked list and see if the exact same word is in the list. However, my console won't stop running and I can't seem to find a problem. I would very much appreciate it if someone can tell me what is wrong with my code.

    public String fiile_Reader() throws FileNotFoundException {
        File file = new File("/Users/djhanz/IdeaProjects/datalab2/pg174.txt"); //reading a plain text file
        Scanner scan = new Scanner(file);
        String fileContent = ""; // initalizing an empty string to put scanned string text file

        while (scan.hasNextLine()) {
            fileContent = fileContent.concat(scan.nextLine() + "\n"); // scan and put in into string object
        }
        fileContent = fileContent.replaceAll("\\p{Punct}", ""); // remove all the punctuation characters
        fileContent = fileContent.toLowerCase();
        return fileContent;
    }

Above is the code that I used to read the file.

public void insert() throws FileNotFoundException{
        Node cursor = head;
        Single_LL Linked_L = new Single_LL();
        String file_content = Linked_L.fiile_Reader();
        String[] splitted_File = file_content.split(" ");

        for(int i=0 ; i<splitted_File.length; i++){
            Linked_L.add(splitted_File[i]);
        }
    }

And this is how inserted into a linked list

  public int Word_Counter(String word){
        String compare =word;
        Node cursor = head;
        int counter = 0;
        while(cursor!=null){
            if (cursor.data.equals(compare)){
                counter++;
                cursor = cursor.next;
            }
        }
        return counter;
    }

I'm having trouble with this word_Counter function.

Below is my test code

public static void main(String[] args) throws FileNotFoundException {
        Program1 test = new Program1();
        String file_content = test.fiile_Reader();
        Single_LL Linked_L = new Single_LL();
        String[] splitted_File = file_content.split(" ");
        int spli_len = splitted_File.length;
        for(int i =0; i< spli_len; i++){

            Linked_L.add(splitted_File[i]);
        }
        int counted = Linked_L.Word_Counter("persian");
        System.out.println(counted);

How can I count the number of words in the list??

Upvotes: 0

Views: 348

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

The problem is with the following code block:

while(cursor!=null){
    if (cursor.data.equals(compare)){
        counter++;
        cursor = cursor.next;
    }
}

If you analyze it carefully, cursor = cursor.next will be executed only when cursor.data.equals(compare) is true; otherwise cursor will not move ahead and thus, cursor!=null will remain true always causing the loop to be infinite.

It should be

while(cursor!=null){
    if (cursor.data.equals(compare)){
        counter++;        
    }
    cursor = cursor.next;
}

i.e. the cursor should move ahead independent of the condition cursor.data.equals(compare).

Upvotes: 1

Marko
Marko

Reputation: 779

You're not moving to the next word unless it matches the comparison word. Change the Word_Counter function to this:

  public int Word_Counter(String word){
        String compare =word;
        Node cursor = head;
        int counter = 0;
        while(cursor!=null){
            if (cursor.data.equals(compare)){
                counter++;
            }
            cursor = cursor.next;
        }
        return counter;
    }

Upvotes: 2

Related Questions