lundi 3 décembre 2018

Problem with Arrays that using for loops and if (JAVA)

im trying to figure out how to get the for loop to search inside all the arrays (I guess the problem is in the for loop named "z" or with the if statement) but it keeps searching just in the first one and then it goes to the else statement of the if when the first city isn't equal to the cityName

Thanks in advance.

public String citiesNorthOf(String cityName)
    {
        String northOfCities = null;
        for(int z = 0 ; z < _noOfCities-1 ; z++)
        {
            if(_cities[z].getCityName().equals(cityName))
            {
                for(int a = 0 ; a < _noOfCities-1 ; a++)
                {

                    Point city1 = _cities[z].getCityCenter();
                    Point otherCity = _cities[a].getCityCenter();
                    if(city1.isUnder(otherCity))
                    {
                        northOfCities = _cities[a].getCityName();
                        System.out.println(northOfCities);
                    }
                    if(northOfCities.equals(null))
                    {
                        String noCitiesNorth = "There is no cities north of "+cityName;
                        return noCitiesNorth;
                    }
                }
            }   

            else
            {
                String noCity = "There is no city with the name: " +cityName;
                return noCity;
            }
        }

        return northOfCities;
    }

Aucun commentaire:

Enregistrer un commentaire