Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at city.routes(city.java:163)
at city.routes(city.java:168)
At first I thought that it was being caused by an infinite loop, but I stepped thru the code and it is triggering the error on the first pass. Here is my code, sorry to send teh whole program but I just don't know enough to narrow down the problem.
/*
*Keith E. Coggin
*outdor@outdorsrealm.net
*AIM: KeithECoggin79
*
*Windows XP/Windows Vista
*jGRASP CSD
*Version 1.8.6_14
*
*IT310 - Data Structures and Algorthms
*Unit 3 - Stacks, Queues, and Recursion
*Sunday, June 7, 2009
*
*Academic Honesty:
*I attest that this is my original work.
*I have not used unauthorized source code, either modified or unmodified.
*I have not given other fellow student(s) access to my program.
*/
//Inport Classes.
import java.io.*;
import java.util.*;
public class city
{
//Properties
String Name;
int X;
int Y;
//Constructors
public city()
{
}
public city(String cName, int cX, int cY)
{
Name = cName;
X = cX;
Y = cY;
}
//Set Methods
public void setName(String iName)
{
Name = iName;
}
public void setX(int iX)
{
X = iX;
}
public void setY(int iY)
{
Y = iY;
}
//Get Methods
public String getName()
{
return Name;
}
public int getX()
{
return X;
}
public int getY()
{
return Y;
}
public static void main(String[] args)
{
//Create Variables.
city tempCity = new city();
LinkedList<city> cityList = new LinkedList<city>();
//create a buffered reader to read input from the keyboard.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//Initiate a try block to catch any errors for the streamreader object.
try
{
//Initilize a loop to prompt the user for one of more additional cities.
boolean cont = true;
while (cont == true)
{
//Prompt the user for the next city.
System.out.println("Please enter the next city(Type \"exit\" to quit.):");
String tempCityName = in.readLine();
if (tempCityName.equalsIgnoreCase("exit"))
{
cont = false;
}
else
{
tempCity.setName(tempCityName);
System.out.println("Please enter the X cordinate for the next city:");
tempCity.setX(Integer.parseInt(in.readLine()));
System.out.println("Please enter the Y cordinate for the next city:");
tempCity.setY(Integer.parseInt(in.readLine()));
//Add the information entered to the appropriate stacks.
cityList.add(tempCity);
}
}
}
//Catch block to complete try block.
catch(IOException e)
{
System.out.println(e.getMessage());
}
routes(null, cityList, 1, 0, null);
}
public static void routes(city sCity, LinkedList<city> lCity, int count, int total, city firstCity)
{
//Declare variables.
String path;
double dist;
city tempCity;
//Test for top level, if yes run recursive.
if (sCity == null)
{
sCity = firstCity = lCity.removeFirst();
total = lCity.size();
count = 0;
}
else if (count == total)
{
if (firstCity.equals(lCity.getFirst()))
{
}
else
{
lCity.addLast(sCity);
sCity = lCity.removeFirst();
count = 0;
}
}
else
{
tempCity = lCity.removeFirst();
path = sCity.getName() + tempCity.getName();
//enter equation here.
lCity.addLast(tempCity);
}
routes(sCity, lCity, count, total, firstCity);
}
}

New Topic/Question
Reply




MultiQuote



|