Reading array of unknown size from file to array

Discussion in 'Java' started by Denis Palas, Dec 19, 2006.

  1. Denis Palas

    Denis Palas Guest

    Dear All

    My program needs to read contents of a file into an array , for example my
    file contains :


    4 3 4
    2 3 1 5
    1 2
    2 4 5 6 7 7

    I deeply appreciate your help with this question :

    How can I set up an array , specially that my column sizes are different as
    you can see , to fill it with contents of a file?


    Thank You
    Denis
    Denis Palas, Dec 19, 2006
    #1
    1. Advertising

  2. Denis Palas

    bugbear Guest

    Denis Palas wrote:
    > Dear All
    >
    > My program needs to read contents of a file into an array , for example my
    > file contains :
    >
    >
    > 4 3 4
    > 2 3 1 5
    > 1 2
    > 2 4 5 6 7 7
    >
    > I deeply appreciate your help with this question :
    >
    > How can I set up an array , specially that my column sizes are different as
    > you can see , to fill it with contents of a file?


    You either need 2 passes, or a more dynamic data structure
    than an array.

    The "obvious" answer is an ArrayList or ArrayLists.

    BugBear
    bugbear, Dec 19, 2006
    #2
    1. Advertising

  3. Denis Palas

    Daniel Pitts Guest

    Denis Palas wrote:
    > Dear All
    >
    > My program needs to read contents of a file into an array , for example my
    > file contains :
    >
    >
    > 4 3 4
    > 2 3 1 5
    > 1 2
    > 2 4 5 6 7 7
    >
    > I deeply appreciate your help with this question :
    >
    > How can I set up an array , specially that my column sizes are different as
    > you can see , to fill it with contents of a file?
    >
    >
    > Thank You
    > Denis

    You would be better off if you also told us what you expected the array
    to look like.

    {4,3,4}
    {2,3,1,5}
    {1, 2}
    {2, 4, 5, 6, 7, 7}

    If thats the case, then you need to read the file one line at a time,
    and then build the array based on the number of tokens. Or use an
    ArrayList (which automatically resizes for you).

    Hope this helps.

    - Daniel.
    Daniel Pitts, Dec 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matthurne
    Replies:
    9
    Views:
    25,988
    Siemel Naran
    Jul 16, 2004
  2. Replies:
    2
    Views:
    5,035
    E. Robert Tisdale
    May 26, 2005
  3. Tonio Cartonio

    Reading a string of unknown size

    Tonio Cartonio, Nov 25, 2006, in forum: C Programming
    Replies:
    111
    Views:
    2,296
    Richard Bos
    Dec 8, 2006
  4. Belinda Lopez

    How to pass a Fix array to unknown size array

    Belinda Lopez, Mar 30, 2008, in forum: C Programming
    Replies:
    0
    Views:
    364
    Belinda Lopez
    Mar 30, 2008
  5. Vincent Arnoux
    Replies:
    1
    Views:
    232
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page