2013-04-03 30 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