Results 1 to 3 of 3
- 11-21-2011, 09:36 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 9
- Rep Power
- 0
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
where the first line indicates the number of nodes in a graphJava Code:3 0 1 1 1 1 1 2 1 1 0 1 1 2 2 0
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
- 11-21-2011, 09:57 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2011, 11:09 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 03:09 PM -
get a token's first character's position in a file
By lgafreitas in forum New To JavaReplies: 1Last Post: 09-01-2010, 01:30 PM -
what's the token for excel file?
By anthrax in forum New To JavaReplies: 3Last Post: 01-27-2010, 07:19 AM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks