I've created a TreeNode class which holds an ArrayList of tree nodes (named branch) and I want to add new branches to the tree by user entered paths. An example path being /Monkey/King/Bar where each of those would ideally be an existing branch with the exception of the last one (Bar would be the branch I'd like to add to King). Temp is a global variable I use for adding new branches to the tree and with recursion I'm trying to move down the path validating that each branch is a child of the previous branch and I'm having some trouble getting it to work. This is what I have so far and was wondering if it has something to do with not setting the parent when I re-declare the temp TreeNode. Any help would be appreciated and if anything I said is too vague please ask for clarification.
TreeNode tree = root;
boolean valid = false;
String y = x; //User entered path
for (int i = 0; i < x.length(); i++)
{
if (x.charAt(i) == '/')
{
for (int j = 0; j < tree.branch.size(); j++){
if (tree.branch.get(j).toString().equals(y)){
System.out.println(temp.value);
tree = tree.branch.get(j);
temp = tree;
valid = true;
}
else
valid = false;
}
y = "";
}