2

I am trying to iterate a list which contains multiple Ids like this:

[148066, 148065, 148064, 148063, 148062, 148061, 148060, 148059, 148058, 148057, 148056]

List<String> list= DbService.getPayUVerificationList();

where this above list object contains all the fetched Ids from DB.

 if(null != list && !list.isEmpty())
 {
     for (int i = 0; i < list.size(); i++) {

            //System.out.println(list.get(i));
            //String txnId= list.get(i).toString();
            //String txnId = (String) list.get(i);
           //String txnId = (String) list.get(i);
            String txnId = (String) list.get(i).toString();
            String output=hdfcService.checkPayStatusByPayU(txnId);
        }
   }

but i am getting

java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String

I have tried lot of casting and approaches but all in vain.

Edit 1: As many are saying that this "list.get(i).toString()" should work but in my case this is still not working and i have no idea about this.

4
  • 3
    String txnId = String.valueOf(list.get(i))? Commented Oct 30, 2018 at 15:56
  • This is nothing but list.get(i).toString() itself, which the OP has already tried. Ideally it should work Commented Oct 30, 2018 at 16:12
  • @nirmalsharma at which line is the error coming? Commented Oct 31, 2018 at 2:41
  • 1
    Possible duplicate of java.math.BigDecimal cannot be cast to java.lang.String? Commented Aug 5, 2019 at 11:35

3 Answers 3

3

String txnId = (String) list.get(i).toString(); should be String txnId = String.valueOf(list.get(i))

Sign up to request clarification or add additional context in comments.

Comments

1

The following should work:

String.valueOf(list.get(i))

Which is essentially nothing but

list.get(i).toString()

So, what you've tried already should work for you.

Comments

1

instead of below, you should not use toString(), its already declared as a "String"

  String txnId = (String) list.get(i).toString();

Do this:

 String txnId = (String) list.get(i);

Or

 String txnId = String.valueOf(list.get(i))

1 Comment

list.get(i).toString() is still not working but later worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.