简体   繁体   English

为什么/在哪里陷入无限循环

[英]Why / where am I getting stuck in an infinite loop

I am currently trying to take a text file, and break the text file into words. 我目前正在尝试提取文本文件,然后将文本文件分解为单词。 I am then attempting to store each word as a node in a binary tree. 然后,我尝试将每个单词存储为二叉树中的一个节点。 After doing so I also try to print the binary tree. 这样做之后,我还尝试打印二叉树。 For some reason when i run my code i am now getting caught in an infinite loop but i don't understand where or why that is if you can see where i am getting caught that would be a great help thanks 由于某些原因,当我运行我的代码时,我现在陷入了无限循环,但是如果您能看到我被困在哪里,我不知道在哪里或为什么这样做,这将是非常有用的帮助,谢谢

public class Tester {

public static void main(String[] args) throws FileNotFoundException {

    Tester run = new Tester();
    run.it();

}

public void it() throws FileNotFoundException { 

    BTree theTree = new BTree();

    String str = this.readInFile();

    int position = 0;

    String newWord = this.breakIntoWords(str, position);

    while(newWord != null){

        theTree.add(newWord);

        newWord = this.breakIntoWords(str, position);

    }

    theTree.print();

}

public String readInFile() throws FileNotFoundException {


    String myFile = "";
    int numWords = 0;

    Scanner myScan = new Scanner(new File("Dracula.txt"));

    while(myScan.hasNext() == true) {

        myFile += myScan.nextLine() + " ";

    }

    return myFile;

}

public String breakIntoWords(String myFile, int position) {

    String nextWord = null;

    char next = myFile.charAt(position);
    next = Character.toLowerCase(next);

    // First trim beginning
    while (((next < 'a') || (next > 'z')) && !Character.isDigit(next)) {

        position++;
        next = myFile.charAt(position);
        next = Character.toLowerCase(next);

    }

    // Now pull only letters or numbers until we hit a space
    while(!Character.isWhitespace(next)) {

        if (Character.isLetterOrDigit(next)) {

            nextWord += myFile.charAt(position);

        }

        position++; 
        next = myFile.charAt(position);

    }

    return nextWord;

}

public class BTree {

private BTNode root;
private int nodeCount;


public boolean add(String word){

    BTNode myNode = new BTNode(word);

    if(root == null){

        root = myNode;
        nodeCount++;
        return true;

    }

    if(findNode(word)){

        int tmp = myNode.getNumInstance();
        tmp++;
        myNode.setNumInstance(tmp);
        return false;

    }

    BTNode temp = root;

    while(temp != null){

        if(word.compareTo(temp.getMyWord()) < 0) {

            if(temp.getRightChild() == null){

                temp.setLeftChild(myNode);
                nodeCount++;
                return true;

            } else {

                temp = temp.getRightChild();

            }

        } else {

                if(temp.getLeftChild() == null){

                    temp.setLeftChild(myNode);
                    nodeCount++;
                    return true;

                } else {

                    temp = temp.getLeftChild();

                }

        }

    }

    return false;

}

public boolean findNode(String word) {
    return mySearch(root, word);
}

private boolean mySearch(BTNode root, String word) {
    if (root == null) {
        return false;
    }

    if ((root.getMyWord().compareTo(word) < 0)) {
        return true;
    } else {
        if (word.compareTo(root.getMyWord()) > 0) {
            return mySearch(root.getLeftChild(), word);
        } else {
            return mySearch(root.getRightChild(), word);
        }
    }
}

public void print() {
    printTree(root);
}

private void printTree(BTNode root) {
    if (root == null) {
        System.out.print(".");
        return;
    }

    printTree(root.getLeftChild());
    System.out.print(root.getMyWord());
    printTree(root.getRightChild());

}

public int wordCount() {

    return nodeCount;

}

You repeatedly call this.breakIntoWords(str, position) with the same str and position , using its return value to decide when to stop. 您使用相同的strposition重复调用this.breakIntoWords(str, position) ,并使用其返回值确定何时停止。 Since nothing changes from the one iteration to the next, the loop never terminates. 由于从一个迭代到下一个迭代没有任何变化,因此循环永远不会终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不确定我如何以及在哪里陷入这个无限循环 - Not sure how and where I am stuck in this infinite loop 为什么我在尝试使用这个 github 项目时陷入无限循环? - Why am I getting stuck in an infinite loop when trying to use this github project? 为什么这个多线程程序陷入无限循环? - Why is this multithreaded program getting stuck at infinite loop? 为什么我输入无效无限循环? - Why am I getting an input Invalid Infinite Loop? 为什么在Java中的此模型中出现无限循环? - Why am I getting an infinite loop in this model in java? 为什么我得到0的无限循环? (Java) - Why am I getting an infinite loop of 0's? (Java) 我在Java输出中遇到无限循环 - I am getting infinite loop in java output ConcurrentHashMap 陷入无限循环 - 为什么? - ConcurrentHashMap stuck in infinite loop - Why? 为什么一个接一个地调用两个Intent服务时出现无限循环? - Why am I getting an infinite loop when calling two Intent services, one after the other? 为什么我的链表赋值的 printList() 方法中出现无限循环? - Why am I getting an infinite loop in my printList() method of my linked lists assignment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM