Hi,

Just wondering if anyone has any example of reading data from a file and storing it into an uneven 2D array.

For example:
the file contains
[a,b,c]
[a,b,c,d]
[a,b,]
etc

I am able to do this with a complete "square" or matrix but when the columns aren't even I'm not sure how to do this.

Code below I initialised the array as 3 rows and 4 columns but my column can be less than that. How do I input the data into the matrix if column is only 2?

File file = new File("testing.txt");
	    
	    String[][] list = new String[3][4];
	    
	    try {
	    	Scanner scanner = new Scanner(file).useDelimiter(",|\r"); 		
	    		for (int r=0; r < 3; r++)
	    		{
	    			for (int c=0; c <4; c++)
	    			{

	    				list[r][c] = scanner.next();
	    			}
	    		}
	    }
	    catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Please help!

Recommended Answers

All 8 Replies

Hi,

Just wondering if anyone has any example of reading data from a file and storing it into an uneven 2D array.

For example:
the file contains
[a,b,c]
[a,b,c,d]
[a,b,]
etc

I am able to do this with a complete "square" or matrix but when the columns aren't even I'm not sure how to do this.

Code below I initialised the array as 3 rows and 4 columns but my column can be less than that. How do I input the data into the matrix if column is only 2?

File file = new File("testing.txt");
	    
	    String[][] list = new String[3][4];
	    
	    try {
	    	Scanner scanner = new Scanner(file).useDelimiter(",|\r"); 		
	    		for (int r=0; r < 3; r++)
	    		{
	    			for (int c=0; c <4; c++)
	    			{

	    				list[r][c] = scanner.next();
	    			}
	    		}
	    }
	    catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Please help!

Here i think you will get some idea !

public class Dynamic_array {
    
    public static void main(String[] args) {
        String file1[][]={{"a","b","c"},{"a","b","c","d"},{"a","b"}}; //Assume that  
                  //this is your file content and you dont know no of rows and 
                  //columns.
        String list[][]=new String[file1.length][]; // After counting no of rows from 
                              //file initialize your row by leaving column as blank
              
  		for (int r=0; r < file1.length; r++)
    		{
                 //Here count the no of columns in your respective row and initialize 
                 //column dynamically
                    list[r]=new String[file1[r].length];
  		  for (int c=0;c<list[r].length; c++)
	    			{
     				  list[r][c] = new String(file1[r][c]);
	    			}
                	}
                
        // output
                 for(int i=0;i<list.length;i++){
                 for(int j=0;j<list[i].length;j++){
                      System.out.print(list[i][j]+" ");
                    }
                    System.out.println();// for new line
                 }
       }
}

For better and easy dynamic initialization you can use ArrayList instead of Array 2d.

Member Avatar for hfx642

The easy answer is...
If you are constructing your 2D array inside your code,
you can use the method that Muralidharan.E pointed out.
If you are reading data from a file,
you have to use the "square" 2D array and hope that you have enough columns.

Stop thinking about 2d arrays. Java arrays are one-dimensional. It's just that the data type for a java array can itself be an array, so you can nest arrays within arrays (etc). It just so happens that a java array whose elements are arrays all of the same length looks like a 2D array, but really that's just a special case, not a starting point.
So you have an array, one element for each line of your file. Each of those elements is itself an array of the tokens from that line.
Now, with that in mind, the code goes like this:
1. Declare array of array of Strings String[][] data; 2. Get the number of lines in the file and use that to allocate the outer array data = new String[noOfLines][]; 3. Read each line and split it into an array of Strings - something like String[] tokens = inputLine.split(","); 4. Put that array into the outer array data[i] = tokens; Remember guys, Java is not C

Stop thinking about 2d arrays. Java arrays are one-dimensional.

Yes. I aware of this.

I have some doubts on java array.

1. Internally array in java is class or what?
2. If array is not a class ,where the array field length has been defined?
3. Is array is anonymous class internally?

Thanks for all your help guys! Managed to get my code working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.