We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All--
I'm trying to write some code that will scan a directory containing an unknown number of txt files, create an array for each txt file and then an array of these arrays. Any ideas on how to do this without being able to declare the size of the arrays? The number of txt files in the directory and the number of lines in each txt file are variables?
I think the File class will do what I need for scanning the directory but I'm having trouble using loadStrings() and a two dimensional String array since the lengths are variables.
Answers
I believe my answer in this recent post is a start:
http://forum.processing.org/two/discussion/10183/about-data-base#latest
Write your own attempt and we'll try to fix it! ;)
The following reads a directory and populates the filenames string with the txt file names:
Now I want to create a two dimensional array files[][] and populate one dimension with the filenames and the other with the lines from the text files obtained by using loadStrings(). I tried the following:
I can't iterate through one dimension of the array while populating the other dimension with loadStrings(). Any ideas?
Let's try to fix it by parts: ~:>
http://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.lang.String-
File dir = new File("C:/Users/XXX/Desktop/Files");
P.S.: Yet it's good practice placing files used by a sketch inside its "/data/" subfolder!
http://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles-java.io.FileFilter-
File[] filenames = dir.listFiles(TXTFILES);
models = new OBJModel[len];
String[][] files
!the # of necessary loadStrings() is already known at this point.
files = new String[len][];
Here's what I've come up w/ after modifying your latest attempt: O:-)
Thanks GoToLoop for all the detail. I tried your code and it works perfectly. That helps A LOT!!!
One question though....why do you use "!=" in your for loop instead of "<". Is it just a style choice or is there a reason why it is necessary?
Style choice and a "dangerous" 1! Only for increments or decrements by 1! :-\"