2013-04-03 28 views
0

我想分析的文本文件,并根据我在addToTree方法中指定的规则,从它建造一个树。然而,即时得到这个错误:arrayOutOfBounds例外的Java

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 
at ie.gmit.TreeTest.addToTree(TreeTest.java:27) 
at ie.gmit.TreeTest.parse(TreeTest.java:20) 
at ie.gmit.TreeTest.main(TreeTest.java:77) 

addChar1和addChar2是我从路过这个词在分析方法

这里创建的节点是代码:

public class TreeTest { 

public void parse(File f) throws Exception { 
    Node root = new Node('+'); //create a root node 
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f))); 

    String line; 
    while((line = br.readLine())!=null){ 
     String[] words = line.toLowerCase().split(" "); 

     for(int i = 0; i < words.length; i++){ 
      addToTree(words[i], root); 
     } 
    } 
} 

public void addToTree(String s, Node root){ 
    char[] characters = s.toCharArray(); 
    Node addChar1 = new Node(characters[0]); 
    Node addChar2 = new Node(characters[1]); 
    Node fullWord = new Node(s); 

    //get the child nodes of the root 
    Node[] rootChildren = root.children(); 
    //get the child nodes of the first node (addChar1) 
    Node[] addChar1Children = addChar1.children(); 

    //get each child of the root 
    for(int i=0; i<rootChildren.length; i++){ 
     Node rootChild = rootChildren[i]; 
     //see if the addChar1 already exists in the tree 
     //if it doesn't 
     if(!rootChild.equals(addChar1)){ 
      //add the addChar1 as a child of the root 
      root.addChild(addChar1); 
      //add the addChar2 as a child of the addChar1 also 
      addChar1.addChild(addChar2); 
      //insert the whole word as the child of the addChar2 
      addChar2.addChild(fullWord); 
     } 
     //if the addChar1 exists in the tree already 
     else{ 
      // get each child of the addChar1 
      for(int j=0; j<addChar1Children.length; j++){ 
       Node addChar1Child = addChar1Children[i]; 
       //see if the addChar2 already exists in the tree 
       //if it doesn't 
       if(!addChar1Child.equals(addChar2)){ 
        //add the addChar2 as the child if the addChar1 
        addChar1.addChild(addChar2); 
        //add the actual word 
        addChar2.addChild(fullWord); 
       } 
       //if the addChar2 exists the the tree already 
       else{ 
        //insert the whole word as the child of the FOUND NODE 
        addChar1Child.addChild(fullWord); 
       } 
      }//end of second for loop 
     } 
    }//end of the first for loop 

}//end of addToTree 

public static void main(String[] args) throws Exception { 
    TreeTest test = new TreeTest(); 

    File f = new File("textFile.txt"); 
    test.parse(f); 
} 

}

任何人都可以帮忙吗? 所有文件包含:

“网站,该网站允许其用户在添加修改或删除通过一般的网页浏览器的内容”

Node类:

public class Node<E> { 

    private Node parent; 
    private String fullWord; 
    private char character; // value inside a node 
    private boolean word; // put a true flag if the node is a word eg 'a' 
    private List<Node> children = new ArrayList<Node>(); //creates a list of array list objects 

    //** constructors **/ 
    public Node(){ 

    } 

    public Node(String fullWord){ 
     this.fullWord = fullWord; 
    } 

    public Node(Node parent){ 
     this.parent = parent; 
    } 

    public Node(char character){ 
     this.character = character; 
    } 

    public Node(boolean word){ 
     this.word = word; 
    } 

    public Node(Node parent, char character){ 
     this(parent); 
     this.character = character; 
    } 

    public Node(Node parent, char character, boolean word){ 
     this(parent); 
     this.character = character; 
     this.word = word; 
    } 

    //** methods **/ 
    public boolean isRoot(){ 
     return this.parent == null; 
    } 

    public boolean hasChildren(){ 
     return this.children.size() > 0; 
    } 

    public void addChild(Node child){ 
     child.setParent(this); 
     children.add(child); 
    } 

    public Node getParent(){ 
     return this.parent; 
    } 

    public void setParent(Node parent){ 
     this.parent = parent; 
    } 

    public Node[] children(){ 
     return (Node[]) children.toArray(new Node [children.size()]); 
    } 

    public char getItem() { 
     return character; 
    } 

}

+3

哪里是线27? –

+0

你确定你的过程不是以某种方式从输入文件中拉空字符串吗?显而易见的罪魁祸首是你的假设,即输入的字符串长度都大于2。 – Perception

+0

实际上所有的文件中的话> = 2的长度 – ciastkoo

回答

0

炭[]字符= s.toCharArray(); //检查字符数组的长度,然后写字符[1]

+0

谢谢。解决 – ciastkoo

0

检查此行(27):

Node addChar1 = new Node(characters[0]); 

请问被传入的包含任何字符?检查你的输入文件没有空行。

0

你或许应该访问它们之前检查指数在0和1:

if (!s.isEmpty()) { 
     char[] characters = s.toCharArray(); 
     Node addChar1 = new Node(characters[0]); 
     Node addChar2 = new Node(characters[1]); 
... 

你显然有一些空字符串。

0

当您使用此代码

Node addChar1 = new Node(characters[0]); 
Node addChar2 = new Node(characters[1]); 

访问索引的元素之前,您应该检查字符数组的大小。当字符阵列大小小于2,那么你将得到ArrayIndexOutOfBoundsException异常

+0

文件中只有1行不包含那些3+长度 – ciastkoo

+0

好了的话,你说,它不包含3+的长度的话。空的单词或有1或2个字符的单词怎么样? – IndoKnight