Hello every one. I have been banging my head with this program. What I need is my printout to be:
Where is 436 is the total miles between the 4 cities, 236 is the distance between Lollardry & scatterling 131 is the distance between scatterling & obstetrist, etc.Code:436 Lollardry 236 scatterling 131 obstetrist 69 provocative
My printout now is:
Code:Lollardry, scatterling, obstetrist, provaocative
Code:public String getRoute(String begin, String end){
boolean beginFound=false;
boolean endFound=false;
for(int i=0;i<array.length;i++){
if(((NodeData)array[i].getFirst()).place.equals(begin)){beginFound=true;}
}
for(int i=0;i<array.length;i++){
if(((NodeData)array[i].getFirst()).place.equals(end)){endFound=true;}
}
if(endFound==false || beginFound==false ){throw new IllegalArgumentException();}
NodeData[] placesToGo = new NodeData[numberOfNodes];
NodeData[] shortestPath = new NodeData[numberOfNodes];
int placesToGoSize=0;
int placesToGoCounter=0;
int shortestPathCounter=0;
NodeData cur = new NodeData(begin, 0);
cur.path=cur.place;
placesToGo[0]=cur; placesToGoCounter++; placesToGoSize++;
while(placesToGoSize!=0){
int posInDataArray=0;
for(int i=0; i<array.length; i++){
if(cur.place.equals(((NodeData)array[i].getFirst()).place)){
posInDataArray=i;
break;
}
}
Iterator iter = array[posInDataArray].iterator();
Object trash = iter.next();
NodeData tmp = null;
while(iter.hasNext()){
tmp=((NodeData)iter.next());
boolean isPresentInShortestPath=false;
for(int i=0; i<shortestPath.length;i++){
if(shortestPath[i]!=null && shortestPath[i].place.equals(tmp.place)){
isPresentInShortestPath=true;
}
}
if(isPresentInShortestPath==false){
int posIfPresent=-1;
boolean isPresentInPlacesToGo = false;
for(int i=0; i<placesToGo.length;i++){
if(placesToGo[i]!=null && placesToGo[i].place.equals(tmp.place)){
isPresentInPlacesToGo=true;
posIfPresent=i;
}
}
if(isPresentInPlacesToGo==true){
int tmpWeight=tmp.weight;
//System.out.println("tmp weight" + tmp.weight);
tmp.weight=tmp.weight+cur.weight;
//System.out.println("tmp weight + cur.weight" + tmp.weight);
tmp.path=cur.path+", "+tmp.place;
if(tmp.weight<placesToGo[posIfPresent].weight){
placesToGo[posIfPresent]=tmp;
}
else{
placesToGo[posIfPresent].weight+=tmpWeight;
//System.out.println(tmpWeight);
}
}
if(isPresentInPlacesToGo==false){
tmp.weight=tmp.weight+cur.weight;
tmp.appendToPath(cur.path+", "+tmp.place);
placesToGo[placesToGoCounter]=tmp;
placesToGoCounter++;
placesToGoSize++;
}
}
}
int smallest=-1;
int smallestPos=-1;
for(int i=0; i<placesToGo.length;i++){
if(smallest==-1 && placesToGo[i]!=null){
smallest=placesToGo[i].weight;
smallestPos=i;
}
if(placesToGo[i]!=null && placesToGo[i].weight<smallest){
smallest=placesToGo[i].weight;
smallestPos=i;
}
}
NodeData smallestData = placesToGo[smallestPos];
placesToGo[smallestPos]=null;
placesToGoSize--;
shortestPath[shortestPathCounter]=smallestData;
shortestPathCounter++;
smallest=-1;
smallestPos=-1;
for(int i=0; i<placesToGo.length;i++){
if(smallest==-1 && placesToGo[i]!=null){
smallest=placesToGo[i].weight;
smallestPos=i;
}
if(placesToGo[i]!=null && placesToGo[i].weight<smallest){
smallest=placesToGo[i].weight;
smallestPos=i;
}
}
if(smallestPos!=-1){cur=placesToGo[smallestPos];}
}
for(int i=0; i<shortestPath.length; i++){
if(shortestPath[i]!=null && shortestPath[i].place.equals(end)){
//System.out.println(shortestPath[i]);
return shortestPath[i].path;
}
}
return "";
}
public String toString(){
String output="";
for(int i=0; i<array.length; i++){
output+=i+1;
Iterator iter = array[i].iterator();
while(iter.hasNext()){
output+="-->"+((NodeData)iter.next()).toString();
}
output+="\n";
}
return output;
}
public class NodeData{
public NodeData(String placeIn, int weightIn){
place=placeIn;
weight=weightIn;
}
public String place="";
public int weight=0;
public String path=place;
public String getPath(){
return path;
}
public void appendToPath(String appendRoute){
path=path+appendRoute;
}
public void appendWeight(int appendWeight){
weight=weight+appendWeight;
}
public void setWeight(int betterWeight){
weight=betterWeight;
}
public void setPath(String betterRoute){
path=place+", "+betterRoute;
}
public String toString(){
return place + " " +weight;
}
}
}
I have been banging my head for a few days and was hoping someone can show me what to do. I would appreciate it and thank!

