Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array[] Size Question

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Question Array[] Size Question

    I have written a simple program that reads in 30 first names from a txt file into an array and 20 surnames from another txt file into another array. All I want to know is, is it possible to create an array with no size specified, have the size defined by the number of names I read in from the txt file.

    Does that make sense?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array[] Size Question

    An array's size is fixed with it is created. You app sounds like it should use an ArrayList
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Array[] Size Question

    If the data in the file varies, then consider setting the size of the array at runtime.

  4. The Following User Says Thank You to Starstreak For This Useful Post:

    StephenCoyle (March 24th, 2013)

  5. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Array[] Size Question

    Quote Originally Posted by Starstreak View Post
    If the data in the file varies, then consider setting the size of the array at runtime.
    Thanks. I was thinking something like that.
    What I have done now, is just created a getFileSize method that takes the file name as a parameter and returns an int (the size of the file).

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Array[] Size Question

    That would give the number of bytes. Is that what you need?
    I was thinking of using a while loop with hasNextLine() to count the lines.

  7. #6
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Array[] Size Question

    I should have mentioned that in my getFileSize method I do have a while loop and a counter. Its the counter that is returned.
    I have it working this way. Thanks for the replies.

Similar Threads

  1. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  2. array size [10] need help!! any experts
    By asdin in forum Collections and Generics
    Replies: 7
    Last Post: February 11th, 2012, 07:17 AM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM

Tags for this Thread