baconbacon baconbacon - 29 days ago 18
Java Question

Index Out Of Bound Exception Error

I've been working on trying to fix this error for a few hours and i cant figure out where/what is causing the error
(java.lang.IndexOutOfBoundsException: index:68 size: 26)

this creates the alphabet in all caps

String [] myStringsChars= new String[26];
for(int i = 0; i < 26; i++)
{
myStringsChars[i] = new String(Character.toChars(i+65));
System.out.println(myStringsChars[i]);

}


i suspect the cause of the problem is one of these two loops

adds array letters to a linked list and sets it as a node

int j=0;
while (j<myStringsChars.length){

BinaryTree.add(alphabet = new TreeNode(myStringsChars[j]));
if (j<=26){
j++;
}
}


sets the node parents and children

int k =0;

while (k<BinaryTree.size()){
int find=(k-1)/2;
BinaryTree.get(k).setParent(BinaryTree.get(find));

if(k%2 ==0){
(BinaryTree.get(k).getParent()). setRightChild(BinaryTree.get(k));
}
else{
(BinaryTree.get(k).getParent()).setLeftChild(BinaryTree.get(k));
}
k++;
}


here's the rest of my code in case it helps

import java.util.*;

public class TreeExercise
{

public static void main(String args[])
{


String [] myStringsChars= new String[26];
for(int i = 0; i < 26; i++)
{
myStringsChars[i] = new String(Character.toChars(i+65));
System.out.println(myStringsChars[i]);

}
List<TreeNode> BinaryTree = new LinkedList();

int j=0;
while (j<myStringsChars.length){

BinaryTree.add(alphabet = new TreeNode(myStringsChars[j]));
if (j<=26){
j++;
}
}
int k =0;

while (k<BinaryTree.size()){
int find=(k-1)/2;
BinaryTree.get(k).setParent(BinaryTree.get(find));

if(k%2 ==0){
(BinaryTree.get(k).getParent()). setRightChild(BinaryTree.get(k));
}
else{
(BinaryTree.get(k).getParent()).setLeftChild(BinaryTree.get(k));
}
k++;
}
BinaryTree.get(0).setParent(null);



Scanner input= new Scanner(System.in);
String userChoice="";
while (!(userChoice.equals("end"))){
System.out.println("enter two CAPITAL letters to find their common ancestor ex.(DC)\n type 'end' to end program");
userChoice= input.nextLine();
char letter1=userChoice.charAt(0);
char letter2=userChoice.charAt(1);
int let1= (int)letter1;
int let2= (int)letter2;
if(userChoice.length()<=2){
// cant find BinaryTree ERROR

TreeNode commonAncestor= findLowestCommonAncestor(root, BinaryTree.get(let1), BinaryTree.get(let2));
if (commonAncestor !=null){
System.out.println(commonAncestor.getContents());
}
System.out.println("Result is: " + "D");
}
else if (userChoice.equals("end")){
System.exit(0);
}
else{
System.out.println("you must type in 2 capital letters");
userChoice=input.nextLine();
}
}
}

public static TreeNode findLowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2)
{
findLowestCommonAncestor(root.getRightChild(), node1, node2)
//every time
TreeNode rightChild= findLowestCommonAncestor(root.getRightChild(), node1, node2);
TreeNode leftChild= findLowestCommonAncestor(root.getLeftChild(), node1, node2);
if (leftChild != null && rightChild!=null){
return root;
}
if(root==null){
return null;
}


if (leftChild!=null){
return leftChild;
}
if(root.getContents()==node1 || root.getContents()==node2){
return root;
}

else {
return rightChild;
}

}
}


TreeNode nodes

public class TreeNode<T extends Comparable>{
private T contents;
private TreeNode<T> parent;
private TreeNode<T> leftChild;
private TreeNode<T> rightChild;
private int level;

public TreeNode()
{
//added
//parent=null;
//leftChild=null;
//rightChild=null;
//level=0;
}
public TreeNode(T data){
contents=data;
this.parent=parent;
}

public TreeNode(T data, TreeNode parent)
{
contents = data;
this.parent = parent;
}

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

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

public boolean isContentEquals(T data)
{
return 0 == getContents().compareTo(data);
}

/**
* @return the contents
*/
public T getContents() {
return contents;
}

/**
* @param contents the contents to set
*/
public void setContents(T contents) {
this.contents = contents;
}

/**
* @return the parent
*/
public TreeNode getParent() {
return parent;
}

/**
* @param parent the parent to set
*/
public void setParent(TreeNode parent) {
this.parent = parent;
}

/**
* @return the leftChild
*/
public TreeNode getLeftChild() {
return leftChild;
}

/**
* @return the rightChild
*/
public TreeNode getRightChild() {
return rightChild;
}
/**
* Given an object T contentToSearch, this method returns
* the node that stores the contentToShare or null if not found on the current tree
* @return the node
*/
public TreeNode findNodeOnTree(T contentToSearch)
{
List<TreeNode> nodes = new LinkedList();
nodes.clear();
nodes.add(this);

while(!nodes.isEmpty())
{
TreeNode current = nodes.remove(0);
if(current.isContentEquals(contentToSearch))
{
return current;
}

if(current.leftChild != null)
{
nodes.add(current.leftChild);
}

if(current.rightChild != null)
{
nodes.add(current.rightChild);
}
}

return null;
}

/**
* @return the level
*/
public int getLevel() {
return level;
}

/**
* @param level the level to set
*/
public void setLevel(int level) {
this.level = level;
}

}

Answer

Your error appears to be here, guessing that this is line TreeExercise.java:113:

int let1= (int)letter1;
int let2= (int)letter2;
if(userChoice.length()<=2){
    // cant find BinaryTree ERROR
    TreeNode commonAncestor= findLowestCommonAncestor(root,
                        BinaryTree.get(let1), BinaryTree.get(let2));
                                       ^^^^                  ^^^^

Your tree list is indexed from 0 to 25, but let1 and let2, given an input of DE, are 68 and 69. So, try:

int let1= (int)letter1 - 'A';
int let2= (int)letter2 - 'A';

It would be clearer in your other code, too, to use 'A' rather than 65.