[prog] Java I/O problem 2

Lisa Wold Eriksen lisaer at stud.ntnu.no
Mon Apr 18 01:43:54 EST 2005


Sitat Dominik Schramm <dominik.schramm at slivery.cotse.net>:

> 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.
[...]
> hope this helps,
> dominik
>
> ps: I didn't want to spoil your programming pleasure, so I didn't
> send
> the whole modified code... :-)


It's also possible to use StringTokenizer:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html
The following example is taken from the above mentioned web page:
[The code:
     StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }
 prints the following output:
     this
     is
     a
     test
]

Hopefully not adding to the confusion...
Lisa





More information about the Programming mailing list