Results 1 to 3 of 3
  1. #1
    kilyx is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default How to read from a file line by line,token by token

    Hi everyone
    i need to read from a file composed like this one
    Java Code:
    3
    0 1 1
    1 1 1
    2 1 1
    0 1
    1 2
    2 0
    where the first line indicates the number of nodes in a graph
    the lines with three numbers indicate the node,the number of outgoing edges,the number of incoming edges
    the lines with two numbers indicate the edges

    in my previous code i used to read the lines checking their lenght :if(str.length()==5),if(str.length()==3),if(str.le ngth()==1)
    off course this checking method doesn't work if I have more than 9 nodes because i'll have some lines like
    '10 x y' and their lenghts are not 1,3 or 5
    so how can i read lines from a file distinguishing the lines with 3 numbers with the lines with 2 numbers?

    thanks to everyone

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

    Default Re: How to read from a file line by line,token by token

    Read an entire line (it's a String) and split on one or more spaces "\\s+"; you get back an array of Strings, not containing spaces; when the array has three elements the original line represented a node/vertex; when the array contains two elements, the line represented an edge. The Integer.parseInt( ... ) method can transform the individual Strings to ints.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kilyx is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: How to read from a file line by line,token by token

    thank you so much!

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. get a token's first character's position in a file
    By lgafreitas in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 01:30 PM
  4. what's the token for excel file?
    By anthrax in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 07:19 AM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 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
  •