I have a GUI that allows records to be entered into a text file, I now need
to perform a search on one of the fields so that the secific record id
displayed in the gui
I have a text field for the search query to be typed in & then a button to
perform the action, however I have no idea of the actual code that does
this. If anyone can help me it would be much appreciated
Dave
Sean Ingham - 01 Jun 2005 12:07 GMT
> I have a GUI that allows records to be entered into a text file, I now need
> to perform a search on one of the fields so that the secific record id
[quoted text clipped - 5 lines]
>
> Dave
What I would do here is to use a bufferedReader to check through your
text file line by line. I'm assuming that each record takes up one line,
and the fields are all seperated by spaces. If this is the case the
split method of String could be used, if you call this method with a
space as a parametre, it will return an array of Strings.
e.g. "ab cd ef gh".split(" "); Gives you an array {ab,cd,ef,gh}
if you know what field you are searching on you can then compare your
input string with the relevant one in the array. All this would be done
in the actionPerformed method for your button.
Pseudo Code
Setup bufferedReader
get input_string from textfield
set int field //which field is being searched
int line_number=0;
while ((line = in.readLine()) != null)
{
String [] stringArray = line.split(" ");
if(stringArray[field].equals(input_string)
{
System.out.println("Match found in record "+line_number);
}
line_number++
}
The code above assumes that your record id is just based on the order
the records are stored. If you're storing the record id as a field in
the record, you can just pick out the relevant field from string_array
when a match is found.
Hope that helps.
Sean Ingham.
Robert Maas, see http://tinyurl.com/uh3t - 11 Jun 2005 06:33 GMT
> From: "Pave" <shankington.j.ramsworthy@twistedtittyrings.com>
> I have a GUI that allows records to be entered into a text file, I
> now need to perform a search on one of the fields so that the secific
> record id displayed in the gui
I'm curious: Why did you choose to re-invent the wheel, write the data
to a flat-text file then do all the parsing of records into fields and
searching on contents of a field yourself, instead of write your
records into a relational database and use SQL queries where all the
search code is already written and nicely debugged? Is this for a class
homework assignment where you aren't allowed to use a database, you are
required to write the search engine yourself? Or is this on a
commercial Unix shell account, not your own machine, so you aren't
allowed to set up your own servers (JDBC or RMI or anything)?
If it's not a class assignment or commercial ISP, so you're allowed to
use a RDBS: J2EE is free for download, and contains CloudScape/Derby,
which runs fine even even when the J2EE server isn't running, so you
can download the whole bundle and just use the part you need and ignore
the rest, assuming you have a few megabytes of disk space available.