Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read data from a file into a 2D array

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Say, I have a file like so:

3
4
1,2,3,4
5,6,7,8
9,10,11,12

The first line represents the number of rows and the second line represents the number of columns. Thereafter, each line represents the data of a 2D array. I am using the following code to create a 2D array from the input file in Java.



The above code works. But, my task is to modify the code in order to create a 2D array from the array data directly without having to specify the rows/columns in the input file. The input file would now look like so:
1,2,3,4
5,6,7,8
9,10,11,12

My code should be able to read this file and dynamically create a 3*4 array. I am not sure how to do that. Thank You!

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way or another, you're going to have to read the whole file first so you know how big your array has to be.

Whether you do multiple passes through it, store it all in memory, use some other data structure as an intermediary, or something else, you HAVE to know the dimensions of the array before you can declare it.

 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does it have to be an array, is there any reason you couldn't use an ArrayList?
 
Greenhorn
Posts: 14
IntelliJ IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I think we can do it by this way.... This is a general Algorithm.

take some initial size of the No_Of_row = 2;
int oldData[] [] = null;
intialCounter = 0;
While(Read the File till EOF){
str := first line of the file.
col[] := split the 'str' by using the specific operator (In Our case its ',')
oldData = new int[No_Of_row][col.length];
initialCounter++;
if(initialCounter > No_Of_row){
int newData[][] = new int[initialCounter][col.length];
System.arrayCopy(oldata,0,newData,0,initialCounter - 1);
}else{
oldData[initialCounter - 1] = col;
}
}


Thanks and Regards
Nirav Joshi
 
The City calls upon her steadfast protectors. Now for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic