Sergio
Sergio

Reputation: 529

Cannot convert from String[] to String

Here is my code.. I want to find just a name of city from latitude and longitude.. Now i get this :

"Address[addressLines=[0:"Mustamäe tee 145",1:"12918 Tallinn",2:"Estonia"],feature=145,admin=Harju County,sub-admin=null,locality=Tallinn,thoroughfare=Mustamäe tee,postalCode=12918,countryCode=Estonia,countryName=null,hasLatitude=true,latitude=59.4123264,hasLongitude=true,longitude=24.6903851,phone=null,url=null,extras=null]"

I tried to convert it to string and split it.. i made a String array but when i try to assign "cityName" to String array "name" I get this error - "Type mismatch: cannot convert from String[] to String"

Can you tell me please where am I wrong?

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSHelper {
static double latitude;
static double longitude;
static String[] name = new String[5];

public static void getCity(LocationManager lm, Context appContext) {

    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    longitude = location.getLongitude();
    latitude = location.getLatitude();
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder userLocation = new Geocoder(appContext, Locale.getDefault());   
    List<Address> cityName;
    try {
        cityName = userLocation.getFromLocation(latitude, longitude, 1);
        if(cityName != null && !cityName.isEmpty()) {
            for(int i=0; i<5; i++){

            name[i] = cityName.get(0).toString().split("");

            }
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

}

Upvotes: 1

Views: 19247

Answers (7)

Kaj
Kaj

Reputation: 10949

cityName.get(0).toString().split("");

That returns a String[] as result, but name[i] should be a String and not a String[]

Why do you split?

Upvotes: 0

Boro
Boro

Reputation: 7943

All the answers here are fine. But what I wonder is why would you use a split on empty character?

Do you know it returns array of single characters (and nothing at start)?

Checkout the code:

String str ="dasd";
String[] strr = str.split("");
for(int i=0; i < strr.length;i++)
    System.out.println("i="+i+";str="+strr[i]);

Output:

i=0;str=
i=1;str=d
i=2;str=a
i=3;str=s
i=4;str=d

I would suggest that since cityName.get(0) returns Address object have a method there which will return you its name, like below.

name[i] = cityName.get(0).getName();

Hope this helps.

Upvotes: 3

Houcine
Houcine

Reputation: 24181

the Split method returns an array of String ; so when you do this :

name[i] = cityName.get(0).toString().split("");

it will return an array of Strings which will contain all of letters of your String ,

Upvotes: 0

acostache
acostache

Reputation: 2275

I do not have any experience with Android, so I don't know what those methods should return, but String.split will return a String array. And name[i] is only a String. So you are trying to assign a String array to a simple String.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691655

The following line is wrong:

name[i] = cityName.get(0).toString().split("");

(as the exception message probably indicates). Indeed, you're trying to initialize name[i], which is a String, with the result of the split method, which returns an array of Strings.

Upvotes: 0

Gnanz
Gnanz

Reputation: 1873

name[i] = cityName.get(0).toString().split("");

here name[i] will hold only string, but split("") method needs String Array in left side.

Is that for loop really needed?? Waht about removing the for loop and code it like

   if(cityName != null && !cityName.isEmpty()) {         

        name[] = cityName.get(0).toString().split("");

    }

Upvotes: 1

amit
amit

Reputation: 178411

name[i] = cityName.get(0).toString().split("");

name[i] is of type String while cityName.get(0).toString().split("") is of type String[]

Upvotes: 2

Related Questions