Your error occurs in line 29 where you attempt to access mydouble[a][b] but you have never initialize mydouble array? This is one of the part that Java spoils those who learn how to program it. The problem is that Java initiates a value to primitive, so some new people assume that it would be the same for array. It is not!
mydouble = new double[x][y]; // where x and y are integer of the size
If you are working on dynamic array, use ArrayList or something else. The array you are using is not working in your dynamic assignment.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
Okay I have a problem now. I need to change the way I want it to be.
Firstly, I want to read the text file, then store all my numbers in the text file into a 2D array.
Secondly, after storing them in a 2D array, then I parse them.
May I know how to do that?
Do you mean that the first 2D array will have the values of the file as Strings and then you will loop the array?
In case you have most of the code. I will post part of what you have done with some suggestions. You must remember first that you can add whatever you want in ArrayLists even arrays:
ArrayList fileList = new ArrayList();
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt")); //reading files in specified directory
String line = null;
while ((line = in.readLine()) != null) {
line = line.trim(); // get rid of trailing spaces: " 1,2,3 " becomes: "1,2,3"
System.out.println("Line is: "+line); // for debuging
String [] arr = line.split(","); // now you have an array of {"1","2","3"}
fileList.add(arr); // the list has all of the arrays
}
// There is a method that converts a list into an array. If the list has values "a", "b", "c" that method will return an array of {"a","b","c"}
// So by using that method you will get an array of arrays, which is a 2D array in java:
String [][] finalArr = (String [][])fileList.toArray();
// each row of the array will have each line. And each column of each row will have the columns of the file
If the method toArray is complicated, once you have the fileList you can do it manually:
int rows = fileList.size(); // lines of the file
String [][] Arr2D = new String[rows][];
for (int i=0;i<rows;i++) {
String [] arr = (String [])fileList.get(i);
Arr2D[i] = arr;
}
Remember 2D arrays in java are an array of arrays. So each element of a 2D array is an array:
String [][] ARR -> 2D array:
ARR[i] is an 1D array
ARR[i][j] is the value of the jth element of the ith array, or the ith row, jth col.
Or:
int rows = fileList.size(); // lines of the file
String [][] Arr2D = new String[rows][];
for (int i=0;i<rows;i++) {
String [] arr = (String [])fileList.get(i);
Arr2D[i] = new String[arr.length];
for (int j=0;j<arr.length;j++) {
Arr2D[i][j] = arr[j];
}
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Oh what about parsing it? You did not include the parsing into double part.
It's not that difficult to add this:
Double.parseDouble in the code, wherever you want it to happen.
You can do it after you got the 2D array of before. You can parse the input, create an 1D double array and add that to the list.
With that code given it's up to you to add the parsing. I am not going to do the whole thing, you need to put some of work. If you want double just create a double array parse the values inside it and then continue using those arrays. Do some thinking on your own.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448