thehoodedsmack
April 9th, 2008, 02:58 PM
So here's the program I'm working on. It's just a simple exercise to test the new stuff we've learned in class. That being recurssion. I keep getting a java.lang.StackOverflowError, so if anyone sees an obvious flaw in my method, please tell me, and I'll be sure to fix it right away. Thanks in advance.
public static void PhoneBookSearch(String someString, int startPage, int endPage)
{
....String[] phoneBookData = new String [26];
....phoneBookData[0] = "accountants";
....phoneBookData[1] = "boats";
....phoneBookData[2] = "contractors";
....phoneBookData[3] = "dog breeders";
....phoneBookData[4] = "entertainers";
....phoneBookData[5] = "farm supplies";
....phoneBookData[6] = "goldsmiths";
....phoneBookData[7] = "hard hats";
....phoneBookData[8] = "ipods";
....phoneBookData[9] = "jazz music";
....phoneBookData[10] = "keys";
....phoneBookData[11] = "land mines";
....phoneBookData[12] = "mortar shells";
....phoneBookData[13] = "nuclear weapons";
....phoneBookData[14] = "oranges";
....phoneBookData[15] = "public services";
....phoneBookData[16] = "quail trainers";
....phoneBookData[17] = "remote controls";
....phoneBookData[18] = "skis";
....phoneBookData[19] = "tree removal";
....phoneBookData[20] = "u-haul";
....phoneBookData[21] = "virus removal software";
....phoneBookData[22] = "waste management";
....phoneBookData[23] = "xylophones";
....phoneBookData[24] = "yard supplies";
....phoneBookData[25] = "zoos";
....if (startPage == endPage)
....{
........if (someString == phoneBookData[startPage])
........{
............System.out.println("Found it! " + someString + " was on page " + startPage + ".");
........}
........else
........{
............System.out.println("Item not in the book");
........}
....}
....else
....{
........int midPage = (endPage-startPage)/2;
........if (someString == phoneBookData[midPage])
........{
............System.out.println("Found it! " + someString + " was on page " + midPage + ".");
........}
........else
........{
............if ((someString).compareTo(phoneBookData[midPage])<0)
............{
................PhoneBookSearch(someString,startPa ge,midPage);
............}
............else
............{
................PhoneBookSearch(someString,midPage ,endPage);
............}
........}
....}
}
public static void main (String[] args)
{
....PhoneBookSearch("nuclear weapons", 0, 25);
}
public static void PhoneBookSearch(String someString, int startPage, int endPage)
{
....String[] phoneBookData = new String [26];
....phoneBookData[0] = "accountants";
....phoneBookData[1] = "boats";
....phoneBookData[2] = "contractors";
....phoneBookData[3] = "dog breeders";
....phoneBookData[4] = "entertainers";
....phoneBookData[5] = "farm supplies";
....phoneBookData[6] = "goldsmiths";
....phoneBookData[7] = "hard hats";
....phoneBookData[8] = "ipods";
....phoneBookData[9] = "jazz music";
....phoneBookData[10] = "keys";
....phoneBookData[11] = "land mines";
....phoneBookData[12] = "mortar shells";
....phoneBookData[13] = "nuclear weapons";
....phoneBookData[14] = "oranges";
....phoneBookData[15] = "public services";
....phoneBookData[16] = "quail trainers";
....phoneBookData[17] = "remote controls";
....phoneBookData[18] = "skis";
....phoneBookData[19] = "tree removal";
....phoneBookData[20] = "u-haul";
....phoneBookData[21] = "virus removal software";
....phoneBookData[22] = "waste management";
....phoneBookData[23] = "xylophones";
....phoneBookData[24] = "yard supplies";
....phoneBookData[25] = "zoos";
....if (startPage == endPage)
....{
........if (someString == phoneBookData[startPage])
........{
............System.out.println("Found it! " + someString + " was on page " + startPage + ".");
........}
........else
........{
............System.out.println("Item not in the book");
........}
....}
....else
....{
........int midPage = (endPage-startPage)/2;
........if (someString == phoneBookData[midPage])
........{
............System.out.println("Found it! " + someString + " was on page " + midPage + ".");
........}
........else
........{
............if ((someString).compareTo(phoneBookData[midPage])<0)
............{
................PhoneBookSearch(someString,startPa ge,midPage);
............}
............else
............{
................PhoneBookSearch(someString,midPage ,endPage);
............}
........}
....}
}
public static void main (String[] args)
{
....PhoneBookSearch("nuclear weapons", 0, 25);
}