Note that there are some explanatory texts on larger screens.

plurals
  1. POarrayOutOfBounds exception java
    text
    copied!<p>I want to parse a text file and build a tree from it according to the rules i specified in the addToTree method. However, im getting this error: </p> <pre><code>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) </code></pre> <p>addChar1 and addChar2 are nodes that I created from the passing in the word in the parse method</p> <p>here is the code:</p> <pre><code>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 &lt; 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&lt;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&lt;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); } </code></pre> <p>}</p> <p>Anyone would help ? all that the file contains:</p> <p>"website which allows its users to add modify or delete its content via web browser usually"</p> <p>the Node class:</p> <pre><code> public class Node&lt;E&gt; { 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&lt;Node&gt; children = new ArrayList&lt;Node&gt;(); //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() &gt; 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; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload