exception - Converting Object to Double in Java -


i'm trying deal lot of classcastexceptions in code originating single critical point. i'll start providing background information question

i'm reading different types of values csv file. following snapshot of class holds them.

public class row { private object[] data;  public object getatindex(int i) {     return data[i]; } } 

for holding different rows of file, i'm using a

arraylist<row> rows; 

the problem lies in function supposed return double[] containing values of particular column stored in arraylist. current function looks 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 cast java.lang.string             result[j]=double.parsedouble(temp);         }         return result;     } 

this getbyindex() throwing classcastexceptions when called. i'm making sure call function columns expected have double values. there other way facilitate object double conversion. progress stalled because of problem. please help.

instead of

temp =(string)rows.get(j).getatindex(i); //java.lang.classcastexception: java.lang.double cannot cast java.lang.string result[j]=double.parsedouble(temp); 

just use following:

        result[j]=(double)rows.get(j).getatindex(i); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -