[resolved, stupid mistake]Problem with Error Checking
I've been working on some code and I've ran into a problem. I have an OakTree (a subclass of the abstract Tree class) with branchs. When I add branchs to it, it is supposed to check to make sure there are no other branchs that are at the same height and faceing the same direction. No matter how many I add it never seems to catch the branch when it there is another just like it.
Here's the code for the running class:
Code:
/**
* Demo class for console applecation for Tree
*
* @authur Jerome Hollon
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class DemoConsole
{
private static ArrayList trees = new ArrayList();
public static void main(String args[]) throws IOException
{
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
int choice;
String strC;
int treeSize = 0;
boolean done = false;
System.out.println("Creating First Tree");
System.out.println("Choose type:\n\t(1) Oak\n\t(2) Exit");
strC = myIn.readLine();
choice = Integer.parseInt(strC);
if(choice == 2)
done = true;
if(choice == 1)
{
boolean correctInput = false;
while(!correctInput)
{
System.out.println("Height of tree?");
strC = myIn.readLine();
treeSize = Integer.parseInt(strC);
if(treeSize <= 1)
System.out.println("Invalid tree Height, must be larger than one.");
else
correctInput = true;
}
OakTree ok = new OakTree(treeSize);
trees.add(ok);
System.out.println("You now have an Oak Tree");
}
while(!done)
{
System.out.println("What do you want to do?");
System.out.println("\t(1)View Trees \n\t(2)Add a Tree\n\t(3)Select a Tree to Edit\n\t(4)Examine Tree\n\n\t(5)Exit");
strC = myIn.readLine();
choice = Integer.parseInt(strC);
if(choice ==5)
done = true;
else if(choice == 1)
viewTrees();
else if(choice == 2)
addTree();
else if(choice == 3)
edit();
else if(choice == 4)
inspect();
else
System.out.println("Incorrect Input");
}
}
private static void viewTrees()
{
for(int i = 0; i < trees.size(); i++)
{
System.out.println("("+(i+1)+")"+((OakTree)trees.get(i)).toString());
}
if(trees.size() == 0)
{
System.out.println("There are no trees");
}
}
private static void addTree() throws IOException
{
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
int choice = 0;
String strC;
int treeSize = 0;
boolean adding = true;
while(adding)
{
System.out.println("What type of tree?");
System.out.println("\t(1) Oak\n\t(2)\n\n\t(5) Cancel");
try{
strC = myIn.readLine();
choice = Integer.parseInt(strC);
}catch(IOException ex)
{
adding = false;
System.out.println("Error in your input");
}
if(choice == 1)
{
boolean correctInput = false;
while(!correctInput)
{
System.out.println("Height of tree?");
try{
strC = myIn.readLine();
treeSize = Integer.parseInt(strC);
}catch(IOException ex)
{
System.out.println("Invalid number for tree height");
}catch(NumberFormatException ex)
{
System.out.println("Invalid number for tree height");
}
if(treeSize <= 1)
System.out.println("Invalid tree Height, must be larger than 1.");
else
correctInput = true;
}
OakTree ok = new OakTree(treeSize);
trees.add(ok);
System.out.println("You added an Oak Tree");
}
if(choice == 5)
adding = false;
else
System.out.println("Invalid Input");
}
}
private static void edit() throws IOException
{
//System.out.println("Debug");
String input="";
int index=0;
int choice=0;
boolean correctInput = true;
boolean cancel = false;
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select tree\t\t\tzz to quit");
viewTrees();
do{
try{
input = myIn.readLine();
index = Integer.parseInt(input);
correctInput = true;
}catch(IOException ex)
{
if(input.equalsIgnoreCase("zz"))
cancel = true;
else
{
System.out.println("Invalid input, must be numeric");
correctInput = false;
}
}
if(index > trees.size())
{
System.out.println("Too large a number, tree not created.");
correctInput = false;
}
}while(!correctInput);
if(!cancel) //there's got to be a better way to do this, maybe as an object?
{
String type = ((Tree)trees.get(index-1)).getTreeType(); //I hope this works
if(type.equals("Oak"))
{
OakTree editing = (OakTree)trees.get(index-1);
System.out.println("\n\t(1)Add Branch\n\t(2)Add leaf");
input = myIn.readLine();
if(input.equals("1"))
addBranch(index-1);
else if(input.equals("2"))
addLeaf(index-1);
else
System.out.println("Invalid input, canceling");
}
}
}
private static void addBranch(int index) throws IOException
{
String choice;
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
Random rand = new Random();
Tree tree = ((Tree)trees.get(index)); //I really hope this works
System.out.println("(1)Random Branch\n(2)Design Branch"); //Just a demo, not everything is used like it could be
choice = myIn.readLine();
if(choice.equals("1"))
{
Boolean done = false;
while(!done)
{
int dir = rand.nextInt(4); //0-3 right?
System.out.println("DIR: " + dir);
int treeSize = tree.getTrunkSize();
//System.out.println("TrunkSize: " + treeSize);
int height = rand.nextInt(treeSize-1)+1;
try{
Branch b = new Branch(dir, height);
tree.growBranch(b);
System.out.println(b.toString());
done = true;
}catch(InvalidPosForBranchException ex)
{
System.out.println("Error occured while trying to put the branch in the first\n"
+"random position, try again? (y/n)");
choice = myIn.readLine();
if(choice.equalsIgnoreCase("y"))
done = false;
else if(choice.equalsIgnoreCase("n"))
done = true; //redundant
else{
System.out.println("Input was neither y or n, assuming y");
done = false;
}
}
}
}
else if(choice.equals("2"))
{
int dir = -1;
int height = -1;
System.out.println("Select a direction, east, west, north, south");
choice = myIn.readLine();
if(choice.equalsIgnoreCase("north"))
dir = 0;
else if(choice.equalsIgnoreCase("south"))
dir = 2;
else if(choice.equalsIgnoreCase("east"))
dir = 1;
else if(choice.equalsIgnoreCase("west"))
dir = 3;
else
System.out.println("Invalid input, selecting direction as north regardless of what you want because you couldn't get it right the first time");
System.out.println("Select a height for the branch between 1 and " + tree.getTrunkSize());
choice = myIn.readLine();
height = Integer.parseInt(choice);
try{
Branch b = new Branch(dir, height);
tree.growBranch(b);
}catch(InvalidPosForBranchException ex)
{
System.out.println(ex.getMessage());
}
}
else
System.out.println("Canceling...");
trees.set(index, tree);
}
private static void addLeaf(int index) //growing random only, don't have time
{
Tree t = (Tree)trees.get(index);
try{
t.growLeaf();
}catch(NoValidBranchException ex)
{
System.out.println(ex.getMessage());
}
trees.set(index,t);
}
private static void inspect() throws IOException
{
String input="";
int index=0;
int choice=0;
boolean correctInput = true;
boolean cancel = false;
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
while(!done)
{
System.out.println("Select tree\t\t\tzz to quit");
viewTrees();
do{
try{
input = myIn.readLine();
index = Integer.parseInt(input);
correctInput = true;
}catch(Exception ex)
{
if(input.equalsIgnoreCase("zz"))
{
cancel = true;
done = true;
}
else
{
System.out.println("Invalid input, must be numeric");
correctInput = false;
}
}
if(index > trees.size())
{
System.out.println("Too large a number, tree not created.");
correctInput = false;
}
}while(!correctInput);
if(!cancel)
{
Tree t = (Tree)trees.get(index-1);
System.out.println(t.toString());
System.out.println("Examine a Branch? (y/n)");
input = myIn.readLine();
if(input.equalsIgnoreCase("y"))
{
int numBranchs = t.getNumBranchs();
ArrayList bs = t.getBranchs();
for(int i = 0; i < numBranchs; i++)
{
System.out.println("("+(i+1)+") " + ((Branch)bs.get(i)).toString());
}
System.out.println("Examine a Leaf? (y/n)");
input = myIn.readLine();
if(input.equalsIgnoreCase("y"))
{
System.out.println("Which Branch?");
input = myIn.readLine();
int newIndex = Integer.parseInt(input);
newIndex--;
if(newIndex < 0 || newIndex > numBranchs)
{
System.out.println("Invalid Input");
correctInput = false;
}
else
correctInput = true;
if(correctInput)
{
Branch b = t.getBranch(newIndex);
int numLeaves = b.getNumLeaves();
for(int i = 0; i < numLeaves; i++)
{
System.out.println("("+(i+1)+") "+b.getLeaf(i).toString());
}
}
}
}
}
}
}
}
Re: Problem with Error Checking
And here is the growBranch method of the OakTree class (It's abstract in Tree class)
Code:
public void growBranch(Branch branch) throws InvalidPosForBranchException //need to add random branch...
{
boolean isEmpty = true;
for(int i = 0; i > branchs.size(); i++)
{
Branch b = (Branch)branchs.get(i);
int inherHei = getBranch(i).getHeight() ;
int inherDir = getBranch(i).getDir();
int newHei = branch.getHeight();
int newDir = branch.getDir();
//if(b.equals(branch))
//if(b.getHeight() == branch.getHeight() && b.getDir() == branch.getDir()) //I don't think the equals will work
if(inherHei == newHei && newDir == inherDir)
{
isEmpty = false;
}
}
if(isEmpty)
{
if(branch.getDir() < 0 || branch.getDir() >3)
throw new InvalidPosForBranchException(branch);
else if(branch.getHeight() > this.getTrunkSize())
throw new InvalidPosForBranchException(branch);
else
branchs.add(branch);
}
else
throw new InvalidPosForBranchException(branch);
}
Note, it compiles with errors if you use a version below 1.5.0
Re: Problem with Error Checking
Ok, here are a couple of ideas that I hope would help
Code:
public class Branch
implements Comparable<Branch>
{
public int compareTo ( Branch o )
{
//Do the comparison to check for height and direction
}
And do the check before you growBranch
Code:
public void growBranch ( Branch b ) throws Exception
{
Iterator<Branch> it = branches.iterator() ;
while ( it.hasNext() )
{
if ( it.next().compareTo( b ) == 0 )
{
throw new Exception() ;
}
}
branches.add( b ) ;
}
Re: Problem with Error Checking
I found my problem, it was about as stupid a mistake as possible, the greater than sign in the for loop in growBranch() should have been less than. I had narrowed the problem down to the for loop, I had relized it wasn't executing so that led me there.