Proper use of for and if statements in Java/Android
I have an two arrays, one is cityUSA[i] and one is decimalUSA[i]. Each has over 1500 entries, cityUSA[100] goes with decimalUSA[100] and so on. I find the city people are in via location services in Android and then I compare it to the list of cities I have in the cityUSA[i] array. I then search for a match and use the i of the match to find the related value of decimalUSA[i] in that array. Here is the code:
loc.getLatitude(); loc.getLongitude(); Geocoder geocoder = new Geocoder(rate.this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
TextView rateText = (TextView)findViewById(R.id.taxRate);
TextView locationText = (TextView)findViewById(R.id.taxLocation);
if(addresses != null) {
开发者_JAVA技巧 Address returnedAddress = addresses.get(0);
String city = returnedAddress.getLocality();
locationText.setText(city);
int i;
for (i = 0; i <= cityUSA.length; i++){
if (cityUSA[i] == city) {
String PrecentString = decimalRate[i];
rateText.setText(PrecentString);
break;
}
}
}
else{
locationText.setText("No City returned!");
rateText.setText("No Rate returned!");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
TextView locationText = (TextView)findViewById(R.id.Rate);
locationText.setText("Cannot get Location!");
}
The application bombs out when I try to run it. If I remove the for statment:
int i;
for (i = 0; i <= cityUSA.length; i++){
if (cityUSA[i] == city) {
String PrecentString = decimalRate[i];
rateText.setText(PrecentString);
break;
}
}
It does not bomb out, but then again it does not perform the search either.
Any suggestions?
Unless you aren't, I see one immediate error in that you're comparing two strings with the "==" operator:
if(cityUSA[i]==city)
Instead, to check the contents of each string (and whether they are equal):
if(cityUSA[i].equals(city))
Also, the <= in the for loop will cause an off by one error. Keep it just as a "<".
Also, what do you mean by "bomb out"? It would be helpful if you gave a specific exception being thrown and on what line.
You are iterating one too many times (of course, this will only happen if the city doesn't exist in the array); Should be for (i = 0; i < cityUSA.length; i++){
.
As to why you may not find the city in the array, you may have a case-sensitivity issue. Maybe try if (cityUSA[i].equalsIgnoreCase(city)){
instead.
Firstly, comparing two string objects using ==
checks whether they are the same object, not whether they contain the same string. You should compare strings with .equals()
.
Secondly, your array index will go out of bounds if the match fails (which, because of the above, it probably will). Valid indices in cityUSA
are 1 to cityUSA.length-1
, whereas the for
loop will continue to cityUSA.length
.
Thirdly, having two codependent arrays in this way is not great design: it ought to be possible to find an Object-oriented pattern suitable to your needs.
精彩评论