Hi everyone,

I'm writing a program that reads numbers from a text file, stores them in a table, does calculations on the numbers and displays the result in a jTable.
The scanner reads the numbers as doubles and stores them in an ArrayList.

My tablemodel is very similar to the one in this link, but I use ArrayList instead of Vector.

My problem is that the getValueAt(int, int) method of my tablemodel returns an object and not a double, so I can't do calculations on the values. How can go back from object to double. I could cast the object to String and than parse the string to a double, but that feels like a terrible way to do it.
Any suggestions?

Think i found the solution myself, when calling the getValueAt(int, int) method you should add (Double)in front of it: (Double)getValueAt(int, int)

It is essential to write double with a capital, that was my mistake.

Ok so from what you have just stated it seems like you are using an ArrayList of Doubles:

ArrayList<Double> data = new ArrayList<Double>();

Right? If not than you should know that the only way to convert a string(from your file) to a double is to use the ValueOf(String s) method in the Double Wrapper class(java.lang.Double). So what I would do is load the file, read it and parse the strings to double and store them in the ArrayList<Double>.

Once you got that than the problem is in the return type of the getValueAt(int, int) method.

public Object getValueAt(int rowIndex, int columnIndex) {
        return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
    }
//This method can be written as:
public Double getValueAt(int rowIndex, int columnIndex){
    return data.get((rowIndex*getColumnCount())+columnIndex);
}

If that doesn't solve your problem, than show us your implementation, there is only so much we can do from making assumptions about your code.

Thanks for the help, I actually tried rewriting the getValueAt() method to return a double, but couldn't get it to work. Your method wouldn't work either in my case :

I used:
ArrayList data = new ArrayList();

instead of:
ArrayList<Double> data = new ArrayList<Double>();

For me data.get() returned an object, so I had to place (Double) in front of it.(see second post)

I see, well casting what the method returns to Double works but its not the optimal thing to do because every time you need to access an object from the array you have to cast what the method returns to double. The optimal thing to do is to convert the strings from the file you are reading into doubles and put them into an ArrayList of Double. Once you have this initialization than my method will work perfectly instead of casting what the original method returns every time you make a call to it.

The only time where returning an Object is a good choice is when you are working with different types of primitives like integers in which case casting the object to the correct type like you are doing would be correct. But even then, there are better ways to achieve that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.