[英]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. 您使用相同的str
和position
重复调用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.