Results 1 to 2 of 2
Thread: array help
- 09-15-2009, 03:44 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
- 09-15-2009, 06:07 AM #2
You will HAVE to know something about the file. For example, you'll need to know what delimiters are being used, or test for several different ones; i.e. spaces, commas, periods, whatever.
Once you know what type of delimiter is used, you can make a 2d array and then read the input line by line.
For file input, I usually use something like the following:
That is an over-simplified example, I'm not checking for errors, and I'm only reading in 1 row. You would need to either make a nested for loop or a while loop that tests for end of file or something like that. I hope it gets you started! :DJava Code:BufferedReader in = new BufferedReader(new FileReader("myFile.txt")); String line = in.readLine(); String[] numbersAsStrings = line.split(","); //creates an array of strings split by the supplied delimiter, in this case a , for(int i=0; i<numbersAsStrings.length; i++){ some2DArray[row][i] = Integer.parseInt(numbersAsStrings[i]); }
Similar Threads
-
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
array or array list?
By Drukqs in forum New To JavaReplies: 5Last Post: 05-06-2009, 06:37 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks