Using ArrayList as a raw type isn't a good idea. You can use Java generics to enforce type checking. In other words you can change it to ArrayList<Double> list = new ArrayList<Double>(); in order to allow only Doubles in the list.
That being said, you can use the Double class's parseDouble method to parse a Double from a String. For example, Double result = Double.parseDouble(myString); . You'll need to be careful because an Exception will be generated if the 'myString' variable cannot be converted to a Double, so you will need to use a try/catch and deal with the condition where it can't be parsed appropriately.
The code you posted before would allow you to insert any type of Object into your ArrayList. However, as I've indicated, it is a better idea to keep the list of one type (Double in this case). Then you can use an add() method which takes a String parameter to add Doubles to your list. For example:
String myString = reader.getValueFromUser();
public boolean add(String str)
{
try {
//parse the Double in here and add to your list
} catch(Exception e){
//do something else here, probably just return 'false'
}
}
Hope that helps. If any of the above didn't make sense, feel free to ask questions, but you should also check out Java Generics, the Double class documentation, and Exceptions.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
The means you can only put that type into the ArrayList. So if you try to put a String into the ArrayList, then yes, you'll get an error. If that doesn't help, you'll need to post your exact error.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
No, you do not have to read it as a String - there are Java library methods that will allow you to read other types from a file. However, that is ignoring the point - that your code, the way you have it now, will work with very minor changes.
Read it in as a String, like you're doing now. Then use the Double.parseDouble() method that I mentioned in my first post to convert the String you just read in into a Double. Then put that Double into your ArrayList using the ArrayList class's add method.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
I guess you are using ArrayList a wrong way...
1)ArrayList is not supposed to hold collection type (array)
2)ArrayList is not suitable to use with multi-dimensional array
3)ArrayList method to get its length is size(). The 'length' is only for primitive array
By the way, I don't know why you need all variables to be static??? Do they still work??? If you are using it just only inside a scope, declare it inside the scope. It is not a good idea to leave local variables as global (as of now for temp, arr, rows, and myDouble).
Not a test code below, but hopefully you would get an idea about how to do it.
import java.io.*;
import java.util.*;
public class Array2 {
static ArrayList<String> myDouble = new ArrayList<String>();
static String[] temp;
static Double[][] finalTemp;
static String[] arr;
public static void main(String args[]) {
try {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt")); //reading files in specified directory
String str = null;
int cols=0;
while ((str = in.readLine()) != null) {
// file reading
// expecting each line has exactly the same format
str = str.trim();
myDouble.add(str);
if (cols==0) { // first time saw a row
temp = str.split(","); // do it once to find out the column number
cols = temp.length;
}
}
in.close(); // you should close the file as soon as you finish reading it!
finalTemp = new Double[myDouble.size()][cols]; // now can initiate array
for (int i=0; i<myDouble.size(); i++) { // myDouble.size() == finalTemp.length
str = myDouble.get(i);
// parse it here
arr = str.split(",");
for (int j=0; j<arr.length; j++) { // arr.length == finalTemp[i].length
finalTemp[i][j] = Double.parseDouble(arr[j]);
}
}
}catch( IOException ioException ) {}
}
}
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
I changed it to size() after you copied my code, I guess... The reason is that ArrayList doesn't have 'length' but size() method instead. I already mentioned that in my previous post as well.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
I guess you are using ArrayList a wrong way...
1)ArrayList is not supposed to hold collection type (array)
Not a test code below, but hopefully you would get an idea about how to do it.
public static void main(String args[]) {
try {
for (int j=0; j<arr.length; j++) { // arr.length == finalTemp[i].length
finalTemp[i][j] = Double.parseDouble(arr[j]);
}
}
}catch( IOException ioException ) {}
}
}
Why isn't it "supposed to be used to hold an array"? An array is an Object and therefore you can use an ArrayList to hold an array. Is it the best idea, usually not, but I'm not sure where you're getting information about that. Also, I thought I should mention that an IOException will not help you if parsing the double fails, since it will throw an InputMismatchException which is *not* an IOException. You either need another catch block (to catch the InputMismatchException) or you need to make your Exception more generic (e.g., Exception)
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Yes, you can add a second catch block right after the first one. The second catch block should catch InputMismatchException
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354