Rodriquez
Rodriquez

Reputation: 1001

Why i got error java.lang.NumberFormatException: Invalid double: "ACE" when Iam have declared a string

I got cards from api: https://deckofcardsapi.com/

First i download a deck, next from this deck i get cards. and here is a problem. When i debug application i got several think like java.lang.NumberFormatException: Invalid double: "ACE", "QUEEN", "JACK", "KING"

but this work when i get:

enter image description here

this is my methods Cards of course i not give getter and setter to better visible

    public class Cards  {
    private boolean succes;
    private List<Card> cards;
    private String deck_id;
    private int remaining;
}

public class Card  {
    private String image;
    private int value;
    private String suit;
    private String code;
}

Here is my method to get Api from server:

 private void getCards(final String deck_id, final int count) {
    retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
    cardApi = retrofit.create(CardService.CardAPI.class);
    cardApi.getCards(deck_id, count)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Cards>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull Cards cards) {
                    Log.d("ARRRRRRRRRRRRAYYYYYYY", String.valueOf(cards));
                    cardsArray = cards.getArrayCards();
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    Log.d("FADSFAFASDFAFS", String.valueOf(e));
                }

                @Override
                public void onComplete() {

                    Toast.makeText(getApplicationContext(), String.valueOf(cardsArray), Toast.LENGTH_SHORT).show();

                }
            });
}

And @Get

  @GET("{deck_id}/draw")
    Observable<Cards> getCards(@Path("deck_id") String deck_id, @Query("count") int count);

And here is JSON

enter image description here

Upvotes: 0

Views: 238

Answers (1)

Zaartha
Zaartha

Reputation: 1126

The value for Card will not always be int. 4 cards will have value "Ace" , "King" ,"Queen" and "Jack". These are strings. That is why you are getting number format exception

public class Card  {
private String image;
private int value;//this will not always be int. 
private String suit;
private String code;
}

To correctly process, the Card class should look like

public class Card{
   private String image;
   private String value; 
   private String suit;
   private String code;
}

Upvotes: 1

Related Questions