File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I/O: searching an address book text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I/O: searching an address book text file" Watch "I/O: searching an address book text file" New topic
Author

I/O: searching an address book text file

N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Howdy all...confused newbie cowpoke here....

I'm stuck on how to proceed with this...any guidance would be appreciated...see my code so far...am I on track?

Thanks in advance!

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You're trying to write the whole program "top-down." This is rarely a good approach, because until you're completely done, you've got nothing. Instead, write it bottom up, iteratively, a little at a time.

1) Write a program which opens a file, reads a line of text, prints it, closes the file. Make sure it works.

2) Write a program which opens a file, reads all the text, prints them, closes the file. Make sure it works.

3) Write a program which takes two Strings as command-line arguments, and prints "OK" if they're equal. Test this too.

4) Write a program which takes one String as a command-line argument, opens a file, reads each line of the file, prints the matching ones. Test.

5) Define a file format for your address book file. Create the file in an editor.

6) Write a program which reads the first and only the first address record from such a file, and prints it out. Test.

7) Now you have examples of all the pieces you need. Write the complete program -- and test it!


[Jess in Action][AskingGoodQuestions]
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Can you clarify what you mean by:

Define a file format for your address book file. Create the file in an editor.


I thought I was to use the text file as is.
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Help...already having difficutly following the advice given...more confused than ever....

What's wrong with my BufferedReader/FileReader statement?

Getting an error about ')' expected ???

Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
It means exactly what it says - you're missing a ")" at the end of
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Oh, BIG DUH...how could I not see that?

Thanks.
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
But I could still use clarification on...


Define a file format for your address book file. Create the file in an editor.


I'm not supposed to access the 'addrbook.txt' file as is?
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
And I'm also trying to see in which one of these six smaller programs I'll be utilizing String Tokeninizing, which my instructor indicated we should be using to split the data into multiple elements.

Thank you. No for me tonight...
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
Headed to the bookstore today to look through Java books to help my confusion...

Can anyone recommend any books with lots of code samples on I/O with VERY CLEAR explanations in everyday language (not programmer to programmer) OR any with examples or exercises of this bottom up approach --building little programs at a time to then use the pieces to write the larger program.
Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
The Java Tutorial has a section on I/O here.

As far as books are concerned, I highly recommend Head First Java.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by N. Gonzalez:

I'm not supposed to access the 'addrbook.txt' file as is?


I'm sorry, N. (I wish you'd use a proper first name -- it's hard to address a person by their initial.) This is my fault. I didn't read closely enough to realize that the problem statement tells you exactly how to store the data (although, actually, I'm not sure it does -- what if the address contains a comma? I guess you can simply ignore that possibility for now.) Ignore my step 5 -- consider it done.
N. Gonzalez
Greenhorn

Joined: Jul 17, 2005
Posts: 29
My name is Nancy. I had to assume that #5 didn't apply.

Actually, because the components of each record are separated by a comma (Last Name, First Name, Address, City, State, Zip, Telephone), that's why I had asked about String Tokenizing. Was unclear on how to go about doing that for each line read and how to refer to only the first token of each line (the last name).

My assignment's due by midnight. I haven't successfully completed all the pieces you suggested, so chances are I won't make my deadline. Hopefully I'll get partial credit for what I do turn in. Bummer...

Thanks for your help.

(By the way, loved the drawing of your daughter on your weblog. I myself am fortunate to have a happy, spirited daughter!)
Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
Hi Nancy - have you looked at the split method (in the String class)? I think this would be easier/more appropriate than StringTokenizer. I don't know what timezone you are in, but chez moi, you still have almost four hours to finish! Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I/O: searching an address book text file
 
Similar Threads
Program on Text Files
unable to exit program..
travel agent - java writing to file via user input
StringTokenizer and End of file question
No such element error