• Post Reply Bookmark Topic Watch Topic
  • New Topic

searching a file  RSS feed

 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I'm very close to completing my very first complete Java application but I ran into a problem that I can't figure out how to solve. So I came here to my favorite place for Java answers.

Here's what I'm doing.

The first part of the program takes three pieces of information from you and slaps it into a file. The information is as follows in this order: Bill name, date due, amount due.

The second part takes a number of days from you and reads the file pulling out all the bills that fit between today's date and the number of days you put in.

I got the first part done without to much diffuculty, it's the second part that I'm having trouble with. I can get today's date, and add the required number of days to it. I can't however seem to figure out how to read the file and retrieve the relevant bills only. I can get all of them but I can't seem to figure out how to search the file by the date.

Any help would be apperciated.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,

Did you store your data as simple text file or random access file? There're some differences in accessing the two kinds of files.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a simple txt file. I didn't want to get to complicated. At least not right away.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congrats!! You have reached the point where you realize that storing data in text files is more trouble than it's worth. It's easier to store data in a small database, even Access will work better than text files

Easiest thing to do is to get the data in a list of beans. You can then search/sort through the beans as required

However, getting the data into the beans is the tricky part.
What's the format of the file? Do you seperate the fields using a delimiter? or are all fields fixed-length?

If data format is delimited, you will have to use a StringTokenizer to read the data into a list of beans. you should be certain that the delimiter is not a character that normally occurs in your data. If it is, then, while saving, you have to encode your data so that the character never occurs, and decode it back while reading

If fixed-length, then you can read the appropriate number of characters for each field into a list of beans. However, you have to make sure that the field sizes are large enough for the data
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first I wanted to store the data into a database to add more functionality but couldn't figure out how to do it. If that way is easier then please inform me how to do it.

As for the file format, here is what I have.

The first field is a string name. This can be of any length.

The second field is an integer. Maximun 2 places. (No day of the month is larger then that.)

The third field is a double. Once again I will not know how long this is because all bills are not the same. That's all the fields.

I do have them entered with a comma and a space. So the format would look like this.

Electric Bill, 23, 74.99
Car Payment, 17, 299.89
etc.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For database, you can use JDBC to store/retreive data from the database. You will have to install the appropriate databse servers, create your tables and use SQL and JDBC to access the data.

For the comma delimited file, can your data contain commas? If yes then you have to encode the commas. Anyways, what you need to do is define a bean for a row of data. Read one line at a time, and then use StringTokenizer to seperate the field values. You will have to trim the extra space from the field values(It might be a good idea not to store the space anyways) Convert the field values into appropriate data types and store the value in an instance of the bean. Store the bean in a list. To search for the data that you want, iterate through the list to find the required data

If you are trying to learn, I would suggest that you complete what you are doing now. You will get an idea of the pitfalls of storing data in a text file. You can change your code to access database when you get around to learning JDBC
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!