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: How do we fill in an array using substring, FileReader and a txt file?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question How do we fill in an array using substring, FileReader and a txt file?

    Got different types of information, which are evenly spaced apart.

    LUONGO ROBERT M 1,69 64,9 2343
    BUSH GEORGE M 1,47 52,2 1941
    GOLDBERG JOHN M 1,89 72,3 2101
    PAULSON MARTIN M 1,73 60,5 2232


  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: How do we fill in an array using substring, FileReader and a txt file?

    Those lines that you have posted can be read into a String (for example with the Scanner class methods)
    Then what do you want done with the data?
    The String class's split method will separate a String at the spaces.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: How do we fill in an array using substring, FileReader and a txt file?

    Quote Originally Posted by Norm View Post
    Those lines that you have posted can be read into a String (for example with the Scanner class methods)
    Then what do you want done with the data?
    The String class's split method will separate a String at the spaces.
    What if there are several spaces between each type of information and there are numbers attached to the names?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How do we fill in an array using substring, FileReader and a txt file?

    Quote Originally Posted by wholegrain View Post
    What if there are several spaces between each type of information and there are numbers attached to the names?
    Read the API docs for Scanner (and java.util.regex.Pattern and Matcher) - it can use regular expressions to cope with variable-sized inputs and delimiting space.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: How do we fill in an array using substring, FileReader and a txt file?

    I'd like to use substring since I already wrote a method to read lines and convert them into strings. Do I need to write a method in my class file? I have no idea how to do this exactly.

  6. #6
    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: How do we fill in an array using substring, FileReader and a txt file?

    The Scanner class would be easier to use to get the words out of the String.
    To use the substring method, you would need to find the location of first byte and the location of last byte of each token and use their values in a substring method call. Finding the first non blank character after the end of a token will require a search loop to loop at each character in turn until you find a non blank. It can be tedious coding.

Similar Threads

  1. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 10:33 AM
  2. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  3. Fill & sort an array
    By yroll in forum Collections and Generics
    Replies: 2
    Last Post: April 1st, 2010, 06:00 AM
  4. Fill array concurrently
    By mamba in forum Collections and Generics
    Replies: 2
    Last Post: October 15th, 2009, 07:08 PM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM