By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

Swing address book problem

P: 6
Hiya everyone. I'm doing a project to code a swing program records basic information on people, with the ability to edit, delete, and view the data, and output the data to a file. I chose a text file for this. To see the format that I used in logging the information, please look at problem1.jpg. I have done all except the edit and delete windows. For both windows, the program will read the data from the file and populate the fields such as

First Name: first_name
Last Name: last_name

and so on. Now the first problem comes from the fact that multiple records are stored in the text file, and the program must go to the next record (forwards/backwards) to edit/delete the data. Is there a way to go to the next record from the record format that I made up? Okay, so look at problem1.jpg again. What I want to do is to move from the first dataset (First Name: Justin...Phone: 7373732) to the next one (Julius...83838) and so on for other entries.

Next is the editing/deleting part. I know how to change the data, problem is, I want to update the current data, and from what I know, if I place the append to the PrintWriter function it will add a new entry to the file. If I remove the append argument, PrintWriter will clear the other records and just write the changes that I specified on the textFields. What I want is to change the data that I want to change and leave the others alone. Now for the delete part, I believe (I haven't start this part) that I would just set the values to " ", just plain spaces, so like

Expand|Select|Wrap|Line Numbers
  1. output.println("First Name: "+fname);
would be
Expand|Select|Wrap|Line Numbers
  1. output.println("");
Correct me if I'm wrong on that one. I believe if I do this successfully it will leave a big gap between the entries that are not deleted. Is there a way to rearrange the data when an entry is deleted?

Okay so what have I done. For populating the fields I did a dirty way of using the scanner, then using skip to "skip" the strings that I don't need then retrieve the unskipped strings to show on the textFields.

e.g.

Expand|Select|Wrap|Line Numbers
  1. Scanner s = new Scanner (new FileInputStream("data.txt"));
  2.       s.skip("First Name: ");
  3.       String fname = s.nextLine();
  4.       //edFname is the name of the textField
  5.       edFname.setText(fname);
and this goes on for every field. Aside from that I have nothing. I'm on a brick wall here, and I've tried all possible ways to address the problem. If you have any problems understanding what I typed, please refer to the attachments below, and if you have additional questions please do ask me. Thank you.
Attached Images
File Type: jpg problem1.jpg (33.8 KB, 67 views)
File Type: jpg problem2.jpg (72.4 KB, 59 views)
File Type: jpg problem3.jpg (27.7 KB, 69 views)
Apr 25 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.