Java Problem

Discussion in 'OT Technology' started by illenvillen008, May 1, 2006.

  1. illenvillen008

    illenvillen008 New Member

    Joined:
    Jan 8, 2006
    Messages:
    205
    Likes Received:
    0
    anone know how to read a string such as
    "jill|smith|1|20 joe|moss|15|2 kyle|kennedy|230|110"
    which is taken from a text file

    and put each part of it into actual data such as

    record[0].name = jill;
    record[0].lname = smith;
    record[0].wins = 1;
    record[0].losses = 20;

    record[0].name = joe;
    record[0].lname = moss;
    record[0].wins = 15;
    record[0].losses = 2;

    record[0].name = kyle;
    record[0].lname = kennedy;
    record[0].wins = 230;
    record[0].losses = 110;


    then convert that back into a string that will be put back into the text file for future reading
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,923
    Likes Received:
    11
    Location:
    Los Angeles
    FYI, I don't think that would work because you'll be overwriting previous data with new entries. You'll need to specify a different memory location in the array.

    I'm not java coder, so I'm not sure if there is a function or library for that, but I would loop through the record and catinate each variable (into a string) from the array into another array which would contain an entry for each person. From there, it would be fairly easy to output the data.
     
  3. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    there are lots of ways, but the best way would probably be to use the split method
    Code:
    String data = "jill|smith|1|20 joe|moss|15|2 kyle|kennedy|230|110";
    String[] tokens = data.split("|");
    
    what this does is separate the words at each "|" and puts it all into a String array so that tokens would equal:

    Code:
    tokens[0] == jill
    tokens[1] == smith
    tokens[2] == 1
    tokens[3] == 20 joe
    tokens[4] == moss
    tokens[5] == 15
    etc. etc.
    
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,482
    Likes Received:
    617
    Use the StringTokenizer class.

    String input = "jill|smith|1|20";
    String delimiter = "|";
    StringTokenizer tokens = new StringTokenizer(input,delimiter);
    while(tokens.hasMoreTokens())
    {
    System.out.println(tokens.nextToken());
    }

    will print:
    jill
    smith
    1
    20

    To work with longer and more complicated input strings, for instance your full string above, you'd need to tokenize the whole string with the default delimiter (a space), then tokenize each token with | as the delimiter.
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,482
    Likes Received:
    617
    HardTech's idea would work just as well. But again, you'd have to first split on the whitespace (to get individual records), then split each token on the | character to get the individual fields.
     
  6. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    I think the StringTokenizer class is being phased out. If you look at the java API, it says to use String.split() for splitting up a string
     

Share This Page