Dominik
Dominik

Reputation: 419

how to check if an if statement inside of a for loop is never used?

i created a Method to check if users location are close to a building.

Therefore i created the following method

private void whereAreYou(final List<location> locations) {
    loadingprogress.setVisibility(View.INVISIBLE);

    for (int i = 0; i < this.cities.size(); i++) {
        final location loc= locations.get(i);
        Location loc1 = mLastLocation;
        Location loc2 = new Location("");
        loc2.setLatitude(loc.getLat());
        loc2.setLongitude(loc.getLng());

        float distanceInMeters = loc1.distanceTo(loc2);
        int distanceInKm = (int) (distanceInMeters / 1000);

        if (distanceInKm <= Integer.parseInt(currentclub.getRadius())) {
            uploadInformationToUserProfile(loc.getName());
        }
    }
}

Now I have to check if a value was within the if statements or not, because then another method must be called, but only if all values ​​do not fit

Upvotes: 0

Views: 106

Answers (2)

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

Add a boolean flag.

private void whereAreYou(final List<location> locations) {
    loadingprogress.setVisibility(View.INVISIBLE);

    boolean locationMatchFound = false; //boolean flag

    for (int i = 0; i < this.cities.size(); i++) {
        final location loc= locations.get(i);
        Location loc1 = mLastLocation;
        Location loc2 = new Location("");
        loc2.setLatitude(loc.getLat());
        loc2.setLongitude(loc.getLng());

        float distanceInMeters = loc1.distanceTo(loc2);
        int distanceInKm = (int) (distanceInMeters / 1000);
        if (distanceInKm <= Integer.parseInt(currentclub.getRadius())) {
            locationMatchFound = true; //boolean flag set to true if location found

            uploadInformationToUserProfile(loc.getName());
        }
    }
    if (!locationMatchFound){
        //Call your other method.
    }
}

Upvotes: 1

John Forbes
John Forbes

Reputation: 1354

You could add a flag that is set to false initially, and within the if statement action code set the flag to true. Following that section of code print it to the console.

Then iterate through the appropriate test cases and check if it ever appears as true.

Upvotes: 2

Related Questions