Converting Object to Double in Java

27,578

Solution 1

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);

Solution 2

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 :)

Share:
27,578
The Mitra Boy
Author by

The Mitra Boy

I'm a graduate student in the MS program of the Department of Computer Science and Engineering at State University of New York at Buffalo. I am a student researcher at the Online Data Interactions Lab at UB. My interests are smartphone databases , database workload study and optimization. I also work in Healthcare IT for enabling research studies through Android applications and Web Services. I have 4 years of work experience in the industry as a software developer and analyst. Recent publications :- Use of Gamified Social Media with Home Telemonitoring for Patient Self-Management in Poorly Controlled Medicaid Diabetics [ Accepted at ACM SIGMIS Computers and People Research 2017 ] I'm an avid Toastmaster. I serve as the Vice President - Public Relations at the University at Buffalo Toastmasters Club. When I'm not working, you'd find me engrossed in stand up comedy, music, geopolitics and fine arts.

Updated on June 05, 2020

Comments

  • The Mitra Boy
    The Mitra Boy almost 4 years

    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.