[prog] Java I/O problem 2
Dominik Schramm
dominik.schramm at slivery.cotse.net
Mon Apr 18 00:22:12 EST 2005
Hi Noir,
Noir <acknak_halflife at yahoo.co.uk> writes:
>> I have a plain-text file and it reads
>> Tony 12.33 45.2 33.33 33.22
>> Jessica 11.3 33.3 12.33 45.5
>> Now, I have to read the file and give the output
>> like
>>
>> Tony
>>
>> 12.33
>> 12.33
>> 45.2
>> 33.33
>> 33.22
>>
>> [...]
>> I can read the file and get the output (STDOUT). But
>> how am I going to break it down into new lines?
>> [...]
to split strings up into their constituents according to a regular
expression, you can use the split method of class String. Here are the
details:
http://java.sun.com/j2se/1.4.2/docs/api/
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
So, in your case, you have a string "record", which is a line
consisting of space separated words. The regular expression is thus
" ", or to catch multiple spaces, " +".
The split operator returns a String[], pseudo-code:
String[] parts = record.split(" +");
hope this helps,
dominik
ps: I didn't want to spoil your programming pleasure, so I didn't send
the whole modified code... :-)
More information about the Programming
mailing list