Search code examples
androiddoublenumberformatexception

java.lang.NumberFormatException: For input string: "-0.‎086167157"


So I have been struggling with this for a while and for me I see no reason why -0.‎086167157 would not be a valid double. I am simply trying to cast a string value that comes back from an API call to a double (longitude value)

Here is the error message:

java.lang.NumberFormatException: For input string: "-0.‎086167157"
W/System.err:     at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
W/System.err:     at java.lang.Double.parseDouble(Double.java:547)
W/System.err:     at com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:909)
W/System.err:     at com.google.gson.Gson$2.read(Gson.java:284)
W/System.err:     at com.google.gson.Gson$2.read(Gson.java:278)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:762)

/

@Parcel(implementations = { LocationRealmProxy.class },
    value = Parcel.Serialization.BEAN,
    analyze = {Location.class})
public class Location extends RealmObject {

@PrimaryKey
@SerializedName("id")
int locationId;

@SerializedName("company_id")
int companyId;

@SerializedName("company_name")
String companyName;

@SerializedName("logo")
String logo;

@SerializedName("address_one")
String addressOne;

@SerializedName("address_two")
String addressTwo;

@SerializedName("address_three")
String addressThree;

@SerializedName("town")
String town;

@SerializedName("county")
String county;

@SerializedName("postcode")
String postcode;

@SerializedName("country")
String country;

@SerializedName("latitude")
Double latitude;

@SerializedName("longitude")
Double longitude;

@SerializedName("venue_type")
String venueType;

@SerializedName("type")
String type;

@SerializedName("added_date")
String addedDate;

@SerializedName("modified_date")
String modifiedDate;

@SerializedName("archived_date")
String archivedDate;

@SerializedName("status")
String status;

@SerializedName("map_thumbnail")
String mapThumbnail;

@SerializedName("venue_type_id")
int venueTypeId;

public Location(){

}

public Location(int locationId, int companyId, String companyName, String logo, String addressOne, String addressTwo, String addressThree,
                String town, String county, String postcode, String country, Double latitude, Double longitude, String venueType, String type,
                String addedDate, String modifiedDate, String archivedDate, String status, String mapThumbnail, int venueTypeId) {
    this.locationId = locationId;
    this.companyId = companyId;
    this.companyName = companyName;
    this.logo = logo;
    this.addressOne = addressOne;
    this.addressTwo = addressTwo;
    this.addressThree = addressThree;
    this.town = town;
    this.county = county;
    this.postcode = postcode;
    this.country = country;
    this.latitude = latitude;
    this.longitude = longitude;
    this.venueType = venueType;
    this.type = type;
    this.addedDate = addedDate;
    this.modifiedDate = modifiedDate;
    this.archivedDate = archivedDate;
    this.status = status;
    this.mapThumbnail = mapThumbnail;
    this.venueTypeId = venueTypeId;
}

//Getters and setters

/

 @GET(NetworkConstants.LOCATIONS_ENDPOINT)
Call<LocationsResponse> getAllLocations(@Header("api-token") String token);

I'm using retrofit with Gson for my network call


Solution

  • Your string has an invisible non printing character just after the period. Copy and paste to a hex dump and you see:

    00000000  2d 30 2e e2 80 8e 30 38  36 31 36 37 31 35 37 |-0....086167157|
    

    It seems to be a left-to-right mark

    Solution: fix it in the API. There's no reason clients should filter the input for garbage in numbers.