Searching a file for a string

O

oliverjb

Hi,

I have a file which contains lots of data in the form:

*Oliver - 1 2 3 4 5 6 7 8 9 0

*Bob - 2 4 6 8 10 12

*Jim - 3 4 5 6 7 etc.

If I wanted to find all the numbers for a particular name, how would I
do it?

So if my argument was Jim, I would want to return 3 4 5 6 and 7 and put
them into an array.

I've looked at DataInputStream and FileInputStream but not quite sure
how I'm supposed to use them.

Many thanks,
Oliver
 
J

j.k.

A simple solution would be to

1) create a regular expression (see java.util.Pattern) that matches the
lines you want to match ("starts with "*Oliver - " and is followed by
your number sequence) and captures the number sequence as a group
2) use java.io.FileReader (wrapped by BufferedReader, so you can use
BufferedReader's readLine method) with the Pattern you created in 1,
and for each line that matches, get the match for the group that
represents the number sequence, and create an array from that using
java.lang.String's split method, which returns an array as you desire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top