java - Why / where am I getting stuck in an infinite loop -


i trying take text file, , break text file words. attempting store each word node in binary tree. after doing try print binary tree. reason when run code getting caught in infinite loop don't understand or why if can see getting caught great 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);      }      // pull letters or numbers until hit 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) same str , position, using return value decide when stop. since nothing changes 1 iteration next, loop never terminates.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -