Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Can't convert an ArrayList into a Double

942741Jun 10 2012 — edited Jun 10 2012
I have this TXT file with some data that i read and insert it to an arraylist then i want to pass the data in to several variables but i keep getting an error when i convert it to double.

TXT file:
volvo
200000
0.7
2000
Mercedes
500000
1.0
1000
Fiat
100000
0.5
5000

Code:
class Register{
private ArrayList bilar = new ArrayList();
private BufferedReader indata = new BufferedReader(new InputStreamReader(System.in));

//Read the information from .txt"
public void läsInRegister() throws IOException{
try
{
FileReader file = new FileReader("C:/Users/Kelly/workspace/cla…
BufferedReader buf = new BufferedReader(file);
String in = buf.readLine();

while (in != null && !in.equals(""))
{
bilar.add(in);
in = buf.readLine();
}
System.out.println(bilar+ "\n" );
}
catch (Exception e){
}
}

public void set(){

for(int i=0; i< bilar.size(); i++) {

String str = bilar.get(i).toString();
String modell = str;
i=i++;
str="";
str = bilar.get(i).toString();
// until here everything is allright

// Now i have try with this ways but still nothing

double inköpspris = Double.parseDouble(str);
i=i++;
double bränsleförbrukning = (double) bilar.get(i);
i=i++;
double underhåll = (double) bilar.get(i);
i=i++;
double bränsleförbrukning = Double.parseDouble((String) bilar.get(i));
}
i can convert it to string there is no problem there, so i tried to convert the string str to double but still keeps getting the same error
the error is:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at examples.Register.sortera(Bilkostnad.jav…
at examples.Bilkostnad.main(Bilkostnad.java…

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 8 2012
Added on Jun 10 2012
1 comment
1,565 views