File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes How to search in a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to search in a text file?" Watch "How to search in a text file?" New topic

How to search in a text file?

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 507

Hi all.

I am trying to read a text file and set its input into some particular JLabels. Have a look at my code.

Here is the image of the output(Attached)

Here are some data in the text file

Graduation Degree Program: Aeronautics-Astronautics School: St.John College First Name: aaaaaaaaaaaa Middle Name: aaaaaaaaaaaa Last Name: aaaaaaaaaaaa City: aaaaaaaaaaaa State: aaaaaaaaaaaa Zip Code: aaaaaaaaaaaa Country: aaaaaaaaaaaa Country Code: aaaaaaaaaaaa Area Code: aaaaaaaaaaaa Residence Phone Number: aaaaaaaaaaaa Office Countr Code: aaaaaaaaaaaa Office Area Code: aaaaaaaaaaaa Office Phone Number: aaaaaaaaaaaa Email Address: aaaaaaaaaaaa Gender: Male Date Of Birth: aaaaaaaaaaaa/aaaaaaaaaaaa/aaaaaaaaaaaa Citizenship Status: U.S. Permanent Citizen Qualification Details:: Program: Aeronautics-Astronautics Grade: Distinction School: St.John College University: Notinham Univercity Test Date: aaaaaaaaaaaa Score: aaaaaaaaaaaa TOEFL Scoreaaaaaaaaaaaa Details for recent Honar and awards: aaaaaaaaaaaa

(I know it is messy! Just added some testing, not the real data. Anyway, this the details of ONE user)

Now, what I need to do is this. I have to SEARCH the data from the text file. Which means when the user selects SCHOOL and PROGRAM and clicks the OK button, relevant details should be searched according to that, and should replaced with the JLABELS I marked in RED in the image. If there are more than one user, then details of those user should be represented in LINE BY LINE according to the SEARCH. Please help me.. I need immediate assistance. Thank you

Are you better than me? Then please show me my mistakes..
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 507

Sorry, image attachment fails. But please help me
Greg Charles

Joined: Oct 01, 2001
Posts: 2967

Wow, that's a lot of code to read! On the other hand, I'd do anything (*) for someone who can spell "its" right.

I think the first thing you want to do is write an effective parser for your file type. The parsing method could take the name of a file, or to be more general, a Reader (which is better for text than an InputStream). It would then read the file and return an object containing the data in an easy to access form. Looking at your data file, I see a lot of key-value pairs, so I'd guess a big part of the returned data would be a HashMap. Maybe that's all you want to return, or maybe there's also separate fields for program, college, and school. That's up to you. Once you get the parser and return class worked out, I think the rest of your application will be much easier to implement.

(*) No, I wouldn't. There are several things I wouldn't do. Still, I'm impressed.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 507

Thanks for the reply. I looked several examples on hashtable, enumeration etc, but I couldn't find an example its for mine. Can you please give me a sample code of reading text using that?
I agree. Here's the link:
subject: How to search in a text file?
It's not a secret anymore!