简体   繁体   中英

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;
    }

The problem is that this code will check the first city, and if it is not the city you are looking for, the code will enter the else block and the return section breaks the loop.

What you are probably after, is that you will need to set a flag to false within the else block. Once that the loop executes, you check to see the status of this flag. If it is false, then, you execute the code currently within your else section.

A similar problem will arise within the following section: if(northOfCities.equals(null)) . However, I am not sure if this is the behaviour you intend.

EDIT: This is more or less what I meant:

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))
                    {
                        return _cities[a].getCityName();    //If we find what we are looking for, we return the name of the city.
                    }
                    if(northOfCities.equals(null))
                    {
                        return "There is no cities north of "+cityName;   //If we find our city, but we also find that there is nothing North of it, we return this error message.
                    }
                }
            } 
        }
        return "There is no city with the name: " +cityName;  //If the for loop has executed and none of the previous return statements have been executed, then, it follows that there is no city with the given name, so we return this error.
    }

如果要处理数组的所有元素,应该使用“ break”而不是“ return”语句,因为“ return”将导致该方法立即退出并返回。

If your the execution goes to this part of the code, this will lead to end the function, because of the return:

if(northOfCities.equals(null))
{
    String noCitiesNorth = "There is no cities north of "+cityName;
    return noCitiesNorth;
}

It is the same for this part:

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

Replace the return elements by break or continue elements

A couple of things:

1. for boundaries

The loops are defined as for(int z = 0 ; z < _noOfCities-1 ; z++) (same for a). It looks like the last citi is not processed. If there are 10 cities and the first one is 0, the last one would be 9. Number of cities minus one would be 9, so when z is 9, as 9 < 9 is false, it won't be processed.

2. northOfCities role is not clear

Being in a loop and the name of the variable suggest it would be a collection of cities, but instead it is a String and it gets overwritten in each matching loop of the a variable. If it actually is assigned a value, it will hold only the last one that matches.

3. Null evaluation

This section of the code seems to me should be outside the a variable loop. In this way it is evaluated after all possible matches have been evaulated. When the loop finishes it makes sense to see if the variable is still null , meaning it hasn't found any match. Also, if the variable is null , calling equals() will throw NPE , so replace that with == .

        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 == null){
                    String noCitiesNorth = "There is no cities north of "+cityName;
                    return noCitiesNorth;
            }
        }  

EDIT I

With the following changes the code should run printing the cities north of a given city. The method will return the name of the last city found, or an error message indicating if the citi is not found or if it doesn't have any cities up north. I had to assuem you have a class Citi , if not just replace the class name with the type of elements stored in the _cities array.

Further enhancements : You could change the method to return a collection of all the cities found, and throw exceptions for the different errors.

public String citiesNorthOf(String cityName){
        String northOfCities = null; //Return value
        City citi = null;
        for(int z = 0 ; z < _noOfCities; z++){
            if(_cities[z].getCityName().equals(cityName)){
                citi = _cities[z];
                for(int a = 0 ; a < _noOfCities ; a++){  
                    Point cityCenter = citi.getCityCenter();
                    Point otherCityCenter = _cities[a].getCityCenter();
                    if(cityCenter.isUnder(otherCityCenter)){
                        northOfCities = _cities[a].getCityName();
                        System.out.println(northOfCities);
                    }
                }
                if(northOfCities == null){
                        northOfCities = "There is no cities north of "+cityName;
                }
            }   
        }
        if(citi == null){
              northOfCities = "There is no city with the name: " +cityName;
        }
        return northOfCities;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM