After some effort (I'm a newbie) I've managed to create a method that reads a text file and puts it into a multi dimensional array. However the array size is hard coded and it doesn't deal with blank lines very well so I have the following questions:
1) How do I define my array to be equal to the size of the text file?
2) Is there an easy way to handle a blank line? Ideally I'd like to just delete any blanks from the text file so that the array contains no null values but I am open to suggestions on the best way to handle it.
Here is what I have so far:
public void TextFileReader (){
String[][] textFileData = new String[5][2];
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
InputSR = new InputStreamReader(getResources().openRawResource(R.raw.text_file));
BufferedRdr = new BufferedReader(InputSR);
try {
// open input stream text_file for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
String[] parts = thisLine.split(" ");
textFileData[i][0] = parts[0];
textFileData[i][1] = parts[1];
Log.v("String Array"+i+"0", String.valueOf(textFileData[i][0]));
Log.v("String Array"+i+"1", String.valueOf(textFileData[i][1]));
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
}
As I said in my comment, using ArrayList in this situation makes things much simpler since you don't have to worry about knowing the size before hand. You can implement it like so:
ArrayList<String[]> arrayList = new ArrayList<>();
try {
// open input stream text_file for reading purpose.
while ((thisLine = BufferedRdr.readLine()) != null) {
if (!thisLine.isEmpty()) {
String[] parts = thisLine.split(" ");
arrayList.add(parts);
}
}
} catch (Exception e) {
e.printStackTrace();
}
Notice that I added the following condition:
if (!thisLine.isEmpty()) {
this makes it so that empty lines are skipped and not added to the ArrayList
.
You can view the contents of your ArrayList
by iterating through it just like you would an array:
for (String[] row : arrayList) {
System.out.println(Arrays.toString(row));
}
Or like so:
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(Arrays.toString(arrayList.get(i)));
}
I made a dummy file with the following data:
line one
line two
line three
Notice the empty lines between each line. Printing the contents of the ArrayList
resulted in:
[line, one]
[line, two]
[line, three]
If you really want to use a double array, you could iterate through the file first and count how many non-empty lines you have just like you're doing so already (with the addition of if (!thisLine.isEmpty()) {
) and then dimension your array like that.
EDIT
Using ArrayList
, if you wanted to get line 2, column 2, you could do it like so (keep in mind arrays start at index 0):
String[] row = arrayList.get(1);
Will grab the row that you want
String value = row[1];
Will grab the column of the row that you want.
Printing value
out with System.out.println(value);
yields in:
two
Or you could just do it in one line:
String value = arrayList.get(1)[1];
Or better yet, you can create a method, so that you can call it any time:
public static String getValue(ArrayList<String[]> arrayList, int row, int column) {
return arrayList.get(row)[column];
}
Which you can call like so:
String value = getValue(arrayList, 1, 1);