How is it possible to store incoming String data from a File into a 2D Array, by means of storing the initial incoming data into

Current steps of project: File f = new File (file path); FileReader r = new FileReader(f); BufferReader b = new BufferReader(r); // split lines from .txt & load into ArrayList String currentLine; while ( (currentLine = b.readLine() != null ) AL.add(currentLine); r.close(); b.close(); for (String s: AL); // This prints each line from txt file to screen just fine Problem: I need to parse and transfer String data from AL or [] (tried both ways) into String[][]; I can parse successfully and get it loaded to the 2D array, however all I can get it to print is the Memory Cell Addresses??? No matter what type of Scanner/Reader I use or initial String holder that is receiving the incoming data from the text file, after moving it to the [][], it will only print the memory cell addresses?? I get that Strings are references and point, but there has to be a way to pull the String Literal from the text file? Plus if they are pointing to raw data, should the String Literal of what it's pointing to print to the Screen?

4/23/2017 9:57:22 PM

Billy Whitchurch

4 Answers

New Answer


Here's an explanation of 2d arrays, hopefully helps for your problem/confusion. If not, please let me know. when you say arr[x] in a 2d array, this is an array. So you will receive the memory location of the array. -> A 2d array is an array of arrays. To get the info you need to say: arr[y][x] y is an array, pointing to the array x. array x is the array that actually contains the values. So if I said arr[1][2] then I'd get the values from the array at index 1 pointing to the array at index 2. So you'd have to print arr[whichArray][indexYouLoadedTheStringsOnto]. If you want to print everything you need a for loop to go over the [whichArray] array.


hm.. String modify = interim.get(i); You seem to be constantly grabbing the same string in the array from inside this for loop: for (int j=0; j < conversions.length; j++) also, a potential problem I spotted: for (int k=j; k < conversions[j].length; k++) { conversions[j][k] = tk2; } This looks to me like your filling an entire array with one value, are you sure need to do that¿ Or, are all arrays different sizes? You also don't fill the entire array. If j is 2 then k = j, so k = 2. conversions[2][2] = tk2. But what was conversions[2][1] ? // (it's null) Perhaps this is what your looking for, or perhaps similar? // note* i moved some variables around String modify, tk1, tk2; int key; for (int i=0; i < interim.size() ; i++) { modify = interim.get(i); key = modify.indexOf(';'); tk2 = modify.substring(key+1); tk1 = modify.substring(0,key); for (int j=1; j < conversions.length; j++) { conversions[i][0] = tk1; conversions[i][j] = tk2; // assuming all i arrays are same size though } } // the printing code looks fine, except perhaps: for (int k=i; k < conversions[i].length; k++) { System.out.print(conversions[i][k] + " "); } // etc... k can start at 0, otherwise your not really printing the whole array. Hopefully something here helps! xD


I finally got it with a bit more playing around! Thank you for the insight. I ended up only using one variable for the iteration process and the two other variables I used as constants to represent what inner array the particular values were to be assigned to. Again, thank you, b/c I would not have figured this out otherwise!!!!


Thank you, that helped me a bit. I'm printing out words now instead of memory addresses, however the only data being stored is the very last line of the txt file. Here's my code that produces that output: for (int i=0; i < interim.size() ; i++) { for (int j=0; j < conversions.length; j++) { String modify = interim.get(i); System.out.println(modify); //check if loaded properly int key = modify.indexOf(';'); String tk2 = modify.substring(key+1); String tk1 = modify.substring(0,key); System.out.println(tk1); System.out.println(tk2); conversions[j][0] = tk1; for (int k=j; k < conversions[j].length; k++) { conversions[j][k] = tk2; } } } for (int i=0; i < conversions.length; i++) { for (int k=i; k < conversions[i].length; k++) { System.out.print(conversions[i][k] + " "); } System.out.println(""); } not quite sure where I'm going wrong. Though looking at a 2d array as "an array of arrays" is a lot more understandable than the way its taught, as a RxC matrix, thank you. So I have tried a few variations of the indexes and placements of where the loading is happening, but I can still only manage to capture the very last line of the txt file. I'm missing something, but have no idea what.