Search code examples
javaarraysstringbigdecimal

Convert BigDecimal to String in Array


The .toString() method does work on BigDecimal, but I can't seem to get it to work when I'm looping through an array and referencing it with array[i]. Here's my code, where numb is a string variable:

for (int i = 0; i < b.length; i++) {
    if((b[i].getClass().toString().contains("String") || b[i].getClass().toString().contains("BigDecimal"))&& isNumeric((String)b[i]) && i != b.length-1){ 
        if(!caught){
            caught = true;
            startIndex = i; //where the number starts
            }
            numb+=b[i].toString();

And the error message:

 Exception in thread "main" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String

Can someone point me to the right direction?


Solution

  • The error message is probably due to this cast:

    (String)b[i]
    

    Replace it with

    b[i].toString()