统计链表中的单词数量

huangapple go评论69阅读模式
英文:

counting number of words in linked list

问题

我正在尝试将一个文本文件放入链表中。我已经成功地将文本文件放入了链表,现在我想要统计特定单词的数量。我的方法是遍历链表,看看列表中是否有完全相同的单词。然而,我的控制台不会停止运行,我似乎找不到问题所在。如果有人能告诉我代码有什么问题,我将不胜感激。

    public String fiile_Reader() throws FileNotFoundException {
        File file = new File("/Users/djhanz/IdeaProjects/datalab2/pg174.txt"); //读取纯文本文件
        Scanner scan = new Scanner(file);
        String fileContent = ""; // 初始化一个空字符串以放置扫描到的文本

        while (scan.hasNextLine()) {
            fileContent = fileContent.concat(scan.nextLine() + "\n"); // 扫描并放入字符串对象中
        }
        fileContent = fileContent.replaceAll("\\p{Punct}", ""); // 移除所有标点符号字符
        fileContent = fileContent.toLowerCase();
        return fileContent;
    }

以上是我用于读取文件的代码。

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]);
        }
    }

这是将内容插入链表的方式。

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;
    }

我在处理这个 Word_Counter 函数时遇到了问题。

以下是我的测试代码:

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);
}

如何统计链表中单词的数量?

英文:

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(&quot;/Users/djhanz/IdeaProjects/datalab2/pg174.txt&quot;); //reading a plain text file
        Scanner scan = new Scanner(file);
        String fileContent = &quot;&quot;; // initalizing an empty string to put scanned string text file

        while (scan.hasNextLine()) {
            fileContent = fileContent.concat(scan.nextLine() + &quot;\n&quot;); // scan and put in into string object
        }
        fileContent = fileContent.replaceAll(&quot;\\p{Punct}&quot;, &quot;&quot;); // 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(&quot; &quot;);

        for(int i=0 ; i&lt;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(&quot; &quot;);
        int spli_len = splitted_File.length;
        for(int i =0; i&lt; spli_len; i++){

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

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

答案1

得分: 2

你只有在单词与比较单词匹配时才会移动到下一个单词。将 Word_Counter 函数更改为以下内容:

  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;
    }
英文:

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;
    }

答案2

得分: 1

问题出在以下代码块:

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

仔细分析,只有当 cursor.data.equals(compare)true 时,才会执行 cursor = cursor.next;否则 cursor 不会向前移动,因此 cursor!=null 将始终为 true,导致循环无限进行。

应该改为:

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

即光标应该在不考虑条件 cursor.data.equals(compare) 的情况下向前移动。

英文:

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).

huangapple
  • 本文由 发表于 2020年10月25日 20:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64523768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定