Can't convert an ArrayList into a Double

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…

They're not doubles, they are Strings, resulting from readLine() calls. You have to parse them into doubles. Your already doing that with one of them, just do it to the others. A trivial problem that you had already solved.

Similar Messages

  • How do you convert this String into a double?

    Hi, in this code I want to convert String arg into a double. I'm trying to do this so that I can convert some string into a double in this calculator that I'm making. Here is the code:
    public class par {
    public static void main(String[] args) {
    String str = "351";
    double arg = new double(str); // should convert the string into a double
         System.out.println(arg);
         System.out.println(str);
    I keep getting errors in line 4, if any of you could help me out here I'll be thankful, i'm kinda new to java and my search for java's equivalent of alof() (its a function in C libraries that converts char[] into a double)lead me to this.
    Tony L.

    Use the Following:
    String num = "123.2";
    double d = Double.parseDouble(num);

  • To convert an ArrayList into a 2-D array

    Is there any efficient and good technique to convert an ArrayList into a 2-D array? Suppose I have an ArrayList that contains objects of my defined class and then I would like to convert this ArrayList into a 2-D array.
    Can someone please help me out here? Your help will be really appreciated!
    Edited by: ti_ma on Mar 18, 2008 4:59 PM

    i think what some people are trying to say is that it would just be fine to use a for loop; i don't know about any magically fast ways of doing it, but it probably won't matter that much unless you're trying to optimize by like nanoseconds...
    it would just be easy and good to do something to the effect of:
    //assuming your ArrayList is called list
    Object[][] arr = new Object[list.size()][3];
    int i = 0;
    for (Book b : list) {
       arr[0] = b.name;
    arr[i][1] = b.price;
    arr[i++][2] = b.isbn;
    hope that helps!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How can I convert  an ArrayList to a String[]

    Hi,
    How can I convert an ArrayList (only with strings) to a String[] ?
    I've tried this :
         public static String listToString(List l) {
              StringBuffer sb = new StringBuffer();
              Iterator iter = l.iterator();
              while (iter.hasNext()) {
                   sb.append(iter.next().toString());
                   if (iter.hasNext()) {
                        sb.append(',');
              return sb.toString();
    But what I get is an array of xxxxx@435634 (for example).
    Thanks a lot !

    Strings are Objects but not all Objects are Strings and at least one of the elements in your List is not a String.

  • How can i convert 3d movie into 2d to watch it in mac

    i have a movie with mkv file playable on vlc how can i convert the movie into 2d

    There are now a bunch of them out there. Do a Google search for "mac 3D conversion" They tend to be around $35. I have been using iSkysoft products, which have been fairly easy to use. They have a "iMedia Converter Deluxe for Mac" that besides the 3D to 2D conversion, will also convert video to 70+ formats, has a 1-Click Video Downloader, and a single click Rip DVD and Burn to DVDs feature. Also claims to be "Ultrafast" all for the same price others apps are that only do the conversion.

  • How can I convert DVD- video into Apple's format so that it can be played and streamed from my Mac?

    How can I convert DVD- video into Apple's format so that I can play the content on my Mac and also stream it to my Apple TV? A few years ago, I transferred all of my home videos onto DVD but now would really like to put these onto my Mac and stream them through Apple TV to watch on the "big screen". Any advice would be most welcome!! Thanks

    MPEG Streamclip.
    Apple do not have their "own" format. You need to convert your DVDs to H.264 though.

  • HT4059 How can I convert a pdf into an ibook?

    How can I convert a pdf into an ibook? I've got a host of PDF's that I'd like to bring into the software but need a quick and simple tool to pull them through. Any ideas?

    In the help of Acrobat look for Form.

  • Can u convert a video into just audio?

    can u convert a video into just audio so i can play it on my nano

    Hi,
    thanks Stefan Maric and wew64.
    in the RTPSocketAdapter.java of the 'Implementing Custom Data Sources and Media Handlers' example I can take the groups of bytes previous to they where send for UDP.
    My Idea its take this bytes, make groups of 259 bytes and send them over the ISDN. In the Clients side recover this information and make groups with the original size (that I must send with the video information over my ISDN connection) and send them with UDP to the same host and receive with a regular JMF application.
    I am not sure if I will have problems with the synchronize of the video and audio doing this process, any idea??
    Thanks a lot
    Ana Cristina

  • How can i convert my pdf into excel

    How can I convert my pdf into excel, the tutorial shows a "content converter" option under the tools section but I do not have that option. I have Acrobat Pro v11.

    With Acrobat XI Pro use this click-path:
    File - Save As Other - Spreadsheet -
    Then select the option desired.
    Be well...

  • Can i convert a pdf into a pgc

    can i convert a pdf into a pgc

    Not with any Adobe software that I know of.  Try if Google finds anything for you.

  • How can I convert a layer into a smart object with Adobe Photoshop Elements 10?

    How can I convert a layer into a smart object with Adobe Photoshop Elements 10?

    That feature is only in PS. Elements as you buy it has only limited support for smart objects. If you use File>Place to add something to your image it will come in as a smart object (within the limits to which PSE understands smart layers). However, if you know someone who can write an action for you in PS you can install it in PSE, or you could look into something like elements+ from simplephotoshop.com.

  • How can I convert page points into document points??

    Hello,
    How can I convert page points into document points? any API?
    I have found 2 APIs
    AIDocumentViewSuite::ArtworkPointToViewPoint( ) and
    AIDocumentViewSuite::ViewPointToArtworkPoint( )
    but I dont know what is view point and art work point.
    I think artwork point = page points and view points = document points. am I correct ???

    What do you mean by 'document points'? Do you mean document units? Like centimeters or inches? If that, there's no API to do (well, kind of in AIUser.h, but that gives you back a string with a unit suffix). Frankly, its pretty easy to just figure out the conversion values for each type of ruler unit -- there are only five or so you need -- and then write a dinky function that takes a value in one or the other, looks up the current document unit and uses that to convert the value (one function for each way). You just need to figure out everything in terms of points (since pixels == points) -- in that regard, it probably helps to know that 72 pt == 1 inch. From that you can figure out pointer-per-inch|centimeter|millimeter|pica.

  • How can i convert my song into .mr4 for making it my ringtone, just renaming it is not working?

    how can i convert my song into .mr4 for making it my ringtone, just renaming it is not working?

    Here's how:
    If you are using Windows, make sure you are able to view the file extension.
    http://support.microsoft.com/kb/865219
    1. Select a title (.mp3 or .m4a) in your iTunes Music Library
    2. Do File > Get Info on selected title (Windows do Edit > Get Info)
    3. In the dialog choose Options tab
    4. Set Start Time and End Time (no more than 35 secs otherwise won't work)
    5. Close dialog.
    6. Now control click the title (right click) and choose Create AAC Version
    Note: If you don't have Create AAC version, switch in menu File > Preferences...(Windows do Edit > Preferences...) > General > Import Settings... > Import Using: AAC Encoder.
    7. When it is done, drag the newly create title to the desktop.
    8. Delete the newly create title in iTunes. <-- This step is important
    9. Rename the title on your desktop from ringtone.m4a to ringtone.m4r
    10. Drag and drop it into iTunes
    Now you have a ringtone.
    Make sure it is selected in iTunes > Tones for syncing with your new iPhone.

  • Can you convert this file into good searchable pdf file: Medical Terminology- A Short Course.azw4

    Can you convert this file into good searchable pdf file: Medical Terminology- A Short Course.azw4

    I don't think that Adobe offers any software to convert .azw4 files.  Search Google for alternate converters.

  • How can you convert a BufferedImage into a RenderedImage?

    I'm using the Histogram class and I'm wondering how I can easily convert my BufferedImage into a RenderedImage so that I can use this class.
    Of course, if I can use my BufferedImage object with the Histogram class, please let me know =D.

    If you have Acrobat 11, you'd select: File > Save As Other > Image
    and select one of the available image formats.

Maybe you are looking for