String Index Out of Bounds异常:字符串索引超出范围

时间:2014-12-16 19:24:54

标签: java binary-tree infinite-loop

我正在拍摄文本文件并将其读入字符串。然后我将字符串分解为单词并将每个单词添加到二叉树中。出于某种原因,我无法在没有收到此错误的情况下到达字符串的末尾:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 867183
at java.lang.String.charAt(String.java:686)
at Tester.breakIntoWords(Tester.java:71)
at Tester.it(Tester.java:21)
at Tester.main(Tester.java:11)

Tester Class(有方法breakIntoWords)

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

    this.breakIntoWords(str, theTree);

    theTree.print();

}

public String readInFile() throws FileNotFoundException {


    String myFile = "";


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

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

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

    }

    return myFile;

}

public void breakIntoWords(String myFile, BTree theTree) {


    String nextWord = "";
    int position = 0;


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

    }

    while(myFile.length() > position) {

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

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

        }

        while(!Character.isWhitespace(next)) {

            if (Character.isLetterOrDigit(next)) {

                nextWord += myFile.charAt(position);

            }

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

        }

        theTree.add(nextWord);
        nextWord = "";

    }

}

}   

BTree Class

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) != null){

        myNode = 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.setRightChild(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 BTNode findNode(String word) {
    return mySearch(root, word);
}

public BTNode mySearch(BTNode root, String word) {
    if (root == null) {
        return null;
    }

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

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

public void printTree(BTNode root) {

    if (root == null) {

        System.out.print(".");
        return;

    }

    if(root.getLeftChild() == null && root.getRightChild() == null) {

        System.out.println(root.getMyWord());

    }

    printTree(root.getLeftChild());
    printTree(root.getRightChild());

}

public int wordCount() {

    return nodeCount;

}
}

BTNode类

public class BTNode {

private BTNode rightChild;
private BTNode leftChild;
private String myWord;
private int numWords;
private int numInstance;
private boolean uniqueWord;
private boolean isRoot;
private boolean isDeepest;

public BTNode(String myWord){

    this.numInstance = 1;
    this.myWord = myWord;
    this.rightChild = null;
    this.leftChild = null;

}

public String getMyWord() {
    return myWord;
}

public void setMyWord(String myWord) {
    this.myWord = myWord;
}

public BTNode getRightChild() {
    return rightChild;
}

public void setRightChild(BTNode rightChild) {
    this.rightChild = rightChild;
}

public BTNode getLeftChild() {
    return leftChild;
}

public void setLeftChild(BTNode leftChild) {
    this.leftChild = leftChild;
}

public int getnumWords() {
    return numWords;
}

public void setnumWords(int numWords) {
    this.numWords = numWords;
}

public boolean isUniqueWord() {
    return uniqueWord;
}

public void setUniqueWord(boolean uniqueWord) {
    this.uniqueWord = uniqueWord;
}

public boolean isRoot() {
    return isRoot;
}

public void setRoot(boolean isRoot) {
    this.isRoot = isRoot;
}

public boolean isDeepest() {
    return isDeepest;
}

public void setDeepest(boolean isDeepest) {
    this.isDeepest = isDeepest;
}

public int getNumInstance() {
    return numInstance;
}

public void setNumInstance(int numInstance) {
    this.numInstance = numInstance;
}

}

2 个答案:

答案 0 :(得分:0)

我不知道该功能在做什么,为什么你需要它而不是string.toLowerCase().split("\\s"),或只是new Scanner(string.toLowerCase())

但是当这个条件为true时:

while(myFile.length() > position)next是一个空格

循环中

position没有增加,next没有被重新分配,所以,你只是继续旋转。

答案 1 :(得分:0)

正如评论中已经提到的,你的一个循环不会返回。有问题的片段是

while(myFile.length() > position) {
  while(!Character.isWhitespace(next)) {
    if (Character.isLetterOrDigit(next)) {
      nextWord += myFile.charAt(position);
    }
    position++; 
    next = myFile.charAt(position);
  }
  theTree.add(nextWord);
  nextWord = "";
}

外部循环执行时myFile.length()positionmyFile.length()已在此处修复,因此position必须通过递增来更改其值。增量完全在一个地方完成:当你在下一个“位置”时有一个空格。如果找到这样的空格,则position的值不会更改,外部循环将永远不会退出。

要从循环返回,即使有空格,也必须增加position的值。