Results 1 to 10 of 10
  1. #1
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default Reading in a line of data and splitting the line up into variables

    So i have several lines of data in a .txt file. I need to read them in and split up each segment into separate variables.

    the data looks like this:

    9#H#Hockey Player 5#1#Red Wings#Goalie#32#600000#0

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for starting a new thread. Now, you'll need to give all your requirements and problems, such as why a split("#") won't work. Best of luck.

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I'm guessing you want to use the # to separate the text?
    The Strings split method might be of use to you.
    Java Code:
    public String[] split(String regex)
        Splits this string around matches of the given regular expression.
    EDIT: sorry missed the post above.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mr.Beans View Post
    I'm guessing you want to use the # to separate the text?
    The Strings split method might be of use to you.
    hm, sounds familiar.

  5. #5
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Ok so i can get it to split now but im now stuck on how i would assign the variables

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by guru32 View Post
    Ok so i can get it to split now but im now stuck on how i would assign the variables
    This doesn't tell us much. You assign them in what ever way works best for you. Without more information, that's the best answer you'll likely get.

    You might want to read this link: How To Ask Questions The Smart Way

    It will help you to ask questions that can be answered in a way that may helpful to you -- for instance how to provide enough information that will allow others to know what your problem is. I know that the site has helped me in the past, and I read it about once a month.

  7. #7
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Oh im sorry.

    What i ment was i can read it all in split it up to what i want and it will print out each segment that i need. Im just not sure how i would be able to store each value as the player number, player name, position etc. Between each # is a variable that needs to be stored

  8. #8
    guru32 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    would i assign them in the while loop is my question

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Probably not since you're not assigning them to an array. Likely you'll have to assign them one at a time, either that or if Player has a constructor that takes either the individual data or the data array, then do it that way.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    but what are you using to read the data?
    if Scanner, then set delimiter and use nextInt, next, etc...
    such as:
    Java Code:
    int playerNumber;
    *loop(){
      playerNumber = scanner.nextInt();
      ...
    }
    *use loop where necessary.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Splitting paragraph into line statements
    By mintu in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 09:41 AM
  2. Data files - how to read random line?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 09:28 PM
  3. remove variables/line in a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-04-2009, 04:05 AM
  4. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:52 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 01:19 AM

Posting Permissions

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