Scheids
Scheids

Reputation: 121

Trying to find min value of two array lists inside a loop

I feel like this shouldn't be a hard fix, but I've been stumped for awhile. Here's the chunk that I'm having issues with.

public static double bestConnectionPrice(ArrayList<String> flightList, String city1, String city2) {
ArrayList<String> firstPlane = new ArrayList<String>();
ArrayList<String> secondPlane = new ArrayList<String>();

double best = 10000;

firstPlane = flightsFrom(flightList,city1);
secondPlane = flightsTo(flightList,city2);
for(int i=0; i<firstPlane.size(); i++) {
    for(int j=0;j<secondPlane.size();j++){
        if(canConnect(firstPlane.get(i), secondPlane.get(j))==true){
                double price = Double.parseDouble(getPrice(firstPlane.get(i))) + Double.parseDouble(getPrice(secondPlane.get(j)));
                    if (price<best) {
                    best=price;
                }
        }
        else
            best = -1;
        }       
    }
return best;
}

It's supposed to find the lowest combined cost of flightsFrom and flightTo. However, it always takes whatever the last price happens to be. Is there any way to set the best variable to whatever it is the first time through the loop and then have it stay as that unless something is smaller than it. Right now it's resetting every time the loops is restarted to I'm not sure how I can compare if something is smaller than it. (I hope that makes sense). Also here is my main method where I'm trying to call it, as well as the other methods that I'm calling just in case.

public static void main(String[] args) throws FileNotFoundException {   
Scanner stdin = new Scanner(System.in);
ArrayList<String> test = new ArrayList<String>();

test.add("Portland#Orlando#200.00");
test.add("Portland#Orlando#100.00"); 
test.add("Portland#Orlando#300.00"); 
test.add("Orlando#Boston#100.00");
test.add("Orlando#Boston#400.00");
test.add("Orlando#Boston#200.00");


double connection = bestConnectionPrice(test,"Portland","Boston");
if (connection<0)
    System.out.println("Sorry, there are no flights between those cities");
else        
    System.out.println("The chepeast price is $"+ connection);
}

public static ArrayList<String> flightsFrom(ArrayList<String> flightList, String city) {
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<flightList.size(); i++){
    String city1 = getOriginationCity(flightList.get(i));
    if (city1.equals(city)) {
        list.add(flightList.get(i));
    }
}
return list;
}

public static ArrayList<String> flightsTo(ArrayList<String> flightList, String city) {
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<flightList.size(); i++){
    String city1 = getDestinationCity(flightList.get(i));
    if (city1.equals(city)) {
        list.add(flightList.get(i));
    }
}
return list;
}    
public static String getPrice(String price) {  //Takes flight description, which is a string, as a parameter and returns price of flight
String[] sArray = price.split("#", -1);
String newPrice = "";
for (int i = 0; i<1; i++)       
    newPrice = sArray[2];
return newPrice;
public static boolean canConnect(String flight1, String flight2) {
String Destination = getDestinationCity(flight1);
String Origination = getOriginationCity(flight2);

if (Destination.equals(Origination)) {
    return true;
}
else
    return false;

   public static String getOriginationCity(String city) {
String[] sArray = city.split("#", -1);
String start = "";
for (int i = 0; i<1; i++)       
    start = sArray[0];
return start;
}

public static String getDestinationCity(String city) {
String[] sArray = city.split("#", -1);
String end = "";
for (int i = 0; i<1; i++)       
    end = sArray[1];
return end;
}

Thanks in advance for any advice you might have!

Upvotes: 0

Views: 57

Answers (1)

Bohemian
Bohemian

Reputation: 425448

Things would be easier if you had a class that represented your string-as-an-object, but nevertheless you are reinventing the wheel:

  1. Make a List of every combination of 1st and 2nd fights
  2. Sort the list based on their combined price using Collections.sort()
  3. The first element in the List is then your result

Upvotes: 1

Related Questions