Results 1 to 5 of 5
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Fetching data from a text file !

    Hi,

    Suppose I have a text file with the following property:

    1) There are different number of columns elements for each row/line. ( For example:

    Row 1: abc , xyz, 1,2,3

    Row 2: xut , 2

    2) Also, the row has different data types.

    My question is, how can I retrieve the contents if I want to use ArrayList and StringTokenizer as the Row length is different?

    Also, can I accomodate different data types using one Arraylist declaration? I'm sure if I'm gonna use Generic form of ArrayList then I won't be able to use two data types as it specifically asks for either <String> or <Integer>

    Please help !

    Thanks

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Fetching data from a text file !

    Is your text file have any data structure? What I mean is from the example you gave above what data that the 1 row represent and 2 row represent? Because if it doesn't have any structure then how you'll know to process these lines of data?

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Fetching data from a text file !

    Hi,

    The text file has contents in the following manner. I'm just enclosing it inside CODE although not required. Just for the sake of clarity:

    Java Code:
    b0, PSA, 12, 79, 68, 184, 125, 179, 133, 189, 138, 139, 229, 131, 225, 127, 233, 21, 175, 26, 168, 18, 163, 67, 73, 74, 76
    b1, OHE, 4, 226, 150, 254, 164, 240, 191, 212, 176
    b2, BHE, 12, 337, 209, 389, 236, 385, 242, 390, 244, 385, 253, 381, 251, 378, 255, 327, 228, 330, 223, 324, 219, 328, 212, 334, 214

    Quote Originally Posted by wsaryada View Post
    Is your text file have any data structure? What I mean is from the example you gave above what data that the 1 row represent and 2 row represent? Because if it doesn't have any structure then how you'll know to process these lines of data?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: Fetching data from a text file !

    If all the fields on a line are separated by commas, you can String.split( ... ) the line ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Fetching data from a text file !

    I am just guessing that the b0..b2 is the line number, and the PSA, OHE, BHE is your data type. By knowing this that you know how to process the data. For example in PSA you'll expect 10 values separated by comma, OHE will contain for example 15 data separated by comma, etc.

    To process the string just use String.split() as mentioned above by Jos.

Similar Threads

  1. Problem fetching data from database using JList
    By LearnJava82 in forum New To Java
    Replies: 3
    Last Post: 05-19-2012, 04:50 PM
  2. loading data from a text file
    By newbie123 in forum JDBC
    Replies: 1
    Last Post: 06-21-2011, 06:09 PM
  3. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  4. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  5. problem with fetching data
    By payal.mitra86 in forum JDBC
    Replies: 0
    Last Post: 05-24-2008, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •