The Mitra Boy
The Mitra Boy

Reputation: 774

Converting Object to Double in Java

I'm trying to deal with a lot of ClassCastExceptions in my code that are originating from a single critical point. I'll start with providing some background information for this question

I'm reading different types of values from a csv file. The following is a snapshot of the class that holds them.

public class Row {
private Object[] data;

public Object getAtIndex(int i)
{
    return data[i];
}
}

For holding the different rows of a file, I'm using a

ArrayList<Row> rows;

The problem lies in a function which is supposed to return a Double[] containing values of a particular column stored in the ArrayList. My current function looks like this

public Double[] getByIndex(int i)
    {
        Double[] result = new Double[rows.size()];
        String temp;
        for(int j=0;j<rows.size();j++)
        {
            temp =(String)rows.get(j).getAtIndex(i); //java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
            result[j]=Double.parseDouble(temp);
        }
        return result;
    }

This getByIndex() is throwing ClassCastExceptions when called. I'm making sure that I call this function for only those columns which are expected to have Double values. Is there any other way to facilitate this Object to Double conversion. My progress is stalled because of this problem. Please help.

Upvotes: 2

Views: 30044

Answers (4)

Gus
Gus

Reputation: 6881

To be somewhat more bulletproof I would combine the answers by esej and vizier... (but only if this wasn't a performance bottle neck!)

Object temp = rows.get(j).getAtIndex(i);
if (temp instanceof Double) {
   result[j]=(Double)rows.get(j).getAtIndex(i);
} else {
  try {
    temp = String.valueOf(rows.get(j).getAtIndex(i));
    result[j]= Double.parseDouble(temp);
  } catch (NumberFormatException e) {
    // logging and recovery code goes here, or rethrow as an exception you can handle.
  }
}

If this is the performance bottleneck of the application then by all means go with what vizier said :)

Upvotes: 1

Tim Pote
Tim Pote

Reputation: 28039

There are a number of problems with what you're attempting.

First and foremost, you shouldn't need a temporary string in order to get to a Double. That's definite code smell, indicating that something's gone wrong.

From the looks of things, you should be able to do that cast directly:

result[j]=(Double)rows.get(j).getAtIndex(i);

This is the suggestion @vizier made

Secondly, if you did want to get a string from some object that is not a string, you should use either Object.toString() or String.valueOf. Simply casting a value in hopes that it's a string is almost never a good idea (unless you're certain that it will be a String). In you're case, you're expecting Doubles, so String.valueOf(Double) would work.

Upvotes: 0

Hakan Serce
Hakan Serce

Reputation: 11256

Instead of

temp =(String)rows.get(j).getAtIndex(i); //java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
result[j]=Double.parseDouble(temp);

just use the following:

        result[j]=(Double)rows.get(j).getAtIndex(i);

Upvotes: 5

esej
esej

Reputation: 3059

Try:

String.valueOf(rows.get(j).getAtIndex(i))

Or: (If your rows only contains Double, but then you should probably declare it as such.)

Double temp;
for(int j=0;j<rows.size();j++)
{
    temp =(Double)rows.get(j).getAtIndex(i);
    result[j]=Double.parseDouble(temp);

You are excplicitly casting something to a String, when that something isn't a String the cast will fail (String is final in Java). The key to the right thing is what you put into the rows, is it only Doubles?

Upvotes: 0

Related Questions