• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Strings

Ratchoss

Newbie
Hello, there is my code :

(It's list of some cities in France)
Java:
  public static String[] Country = new String[]{"Antony", "Châtenay-Malabry", "Sceaux", "Bourg-la-Reine", "Bagneux", "Fontenay-aux-Roses"
            , "Le Plessis-Robinson", "Clamart", "Châtillon", "Montrouge", "Malakoff", "Vanves", "Issy-les-Moulineaux", "Boulogne-Billancourt",
            "Meudon", "Sèvres", "Chaville", "Ville-d'Avray", "Saint-Cloud", "Marnes-la-Coquette", "Vaucresson", "Garches", "Rueil-Malmaison",
            "Suresnes", "Puteaux", "Nanterre", "Colombes", "La Garenne-Colombes", "Bois-Colombes", "Courbevoie", "Neuilly-sur-Seine", "Levallois-Perret",
            "Clichy", "Asnières-sur-Seine", "Gennevilliers", "Villeneuve-la-Garenne",
            "Paris 75001", "Paris 75002", "Paris 75003", "Paris 75004", "Paris 75005", "Paris 75006", "Paris 75007", "Paris 75008", "Paris 75009",
            "Paris 75010", "Paris 75011", "Paris 75012", "Paris 75013", "Paris 75014", "Paris 75015", "Paris 75016", "Paris 75017", "Paris 75018",
            "Paris 75019", "Paris 75020",};

Java:
   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // retrive the data by using getPlace() method.
                Place place = PlaceAutocomplete.getPlace(this, data);
                showAddress();
                streetAddress.clear();
                adresse.setText(place.getAddress());
                streetAddress.add("" + place.getAddress());
                villeAdresse();
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                // TODO: Handle the error.
                //

            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }

Java:
 private void villeAdresse() {
        if(streetAddress.contains(Country)) {
            laville.clear();
            laville.add(Country);
        }
    }

So, we have 2 lists: "streetAddress" and "laville".

The "streetAddress" list will allow us to add the exact address entered by the user in GooglePlace Autocomplete.

The list "laville" will then allow us to retrieve only the city of the address that has been registered in the list "streetAddress".

Now, I would like to check: if the word "Puteaux" is registered in the list "streetAddress" then we add it automatically to the list "laville".

So I am stuck on this subject, could someone help me? thank you in advance.

If you want to know more, please let me know by comment.
 
You've not shown the definition of streetAddress, but I assume it's this

Code:
List<String> streetAddress = new ArrayList<String>();

Based on the above assumption, you can check if the list contains any String value, by using the contains() method. So I could write

Code:
if (streetAddress.contains("Puteaux")) {
 // do something
}
 
You've not shown the definition of streetAddress, but I assume it's this

Yes that's it.

Based on the above assumption, you can check if the list contains any String value, by using the contains() method. So I could write
Java:
if (streetAddress.contains("Puteaux")) {
// do something
}

The problem is i want to check if streetAddress contains any string from String[] Country... It'll take too long to check all Country's value.
 
hello, so I did what you told me, but unfortunately there is a problem, I had to add some "debug message", and the only message displayed is "Not contains". So there is a problem, no?

For example, streetAddress contains = 15 rue de la villiardiere, 92800, Puteaux, France.

Now I want to check if a word in the list "streetAddress" is also in the list of cities (Country).

Java:
    private void villeAdresse() {
        for (String country : Country) {
            if (streetAddress.contains(country)) {
                Toast.makeText(getApplicationContext(), "contains", Toast.LENGTH_LONG).show();
                laville.clear();
                laville.add(country);
                Toast.makeText(getApplicationContext(), laville.toString(), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "not contains", Toast.LENGTH_LONG).show();
            }
        }
    }

If you have any question to help this problem to be solved, let me know please. thanks.
 
Right. Let me ask you this question: How many items does the streetAddress list have? If it's only ONE, having the value of "15 rue de la villiardiere, 92800, Puteaux, France", then this code will work:

Code:
for (String country : Country) {
  if (streetAddress.get(0).contains(country)) {
    // It matches!!
  }
  ...
}
 
However, if streetAddress has multiple entries, then you need a nested for loop, something like this:

Code:
for (String country : Country) {
  for (String address : streetAddress) {
    if (address.contains(country)) {
      // It matches!!
    }
    ...
  }
}

In fact this is a more general solution that handles both cases, so I would go with that.
 
Thank you, but there is one more problem. When I did exactly what you said, it still does not work. Look at the debug messages.

Code:
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // retrive the data by using getPlace() method.
                Place place = PlaceAutocomplete.getPlace(this, data);
                showAddress();
                streetAddress.clear();
                adresse.setText(place.getAddress());
                streetAddress.add("" + place.getAddress());
                villeAdresse();
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);

            } else if (resultCode == RESULT_CANCELED) {
            }
        }
    }

    private void villeAdresse() {

        for (String country : Country) {
            if (streetAddress.get(0).contains(country)) {
                laville.clear();
                laville.add(country);
                Toast.makeText(getApplicationContext(), laville.toString(), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "not contains", Toast.LENGTH_LONG).show();
            }
        }
    }

The debug message "not contains" is the one currently displayed.

So the first element in the list "streetAddress" is: "15 rue de la villiardiere, 92800, Puteaux, France" and I just want to get the name of the city and add it to the list "laville".

Thank you very much for your help, it's rare nowadays people like you! Thank you.
 
You can do a lot to help yourself in this situation by getting familiar with running your app in debug mode. Problems like this involve interactive debugging, and it's not something which really can be done via a Q&A forum such as this.
If you run your app in debug mode, it then allows you to set breakpoints in the code, look at the values of variables, and step through execution, line by line. Doing this will allow you to see what the problem is, and hopefully resolve it.
I can't offer any further advice, because I'd have to go through exactly the process I described above. You can only do so much with static analysis of code. At some point you have to run it, and do interactive debugging. Without that, it's just guesswork I'm afraid.
 
Back
Top Bottom