Need help for making an address book using Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Need help for making an address book using Java

 
0
  #1
Oct 21st, 2008
I am a beginner at Java.
I have been given an assignment to make a simple address book(without applets) using Java.
It should have the following functions:-
1) search-by name,city.
2)add-name,telephone number,city,address.
3)modify.
4)delete.

I am still working on the program.But the problem comes when the screen(black one) becomes too crowded(main menu has to be repeated to ask the user what to do next).

So is there any function to clear the screen before?

Please inform me about it as soon as possible.
Thank you,
Shubhang.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 806
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: Need help for making an address book using Java

 
0
  #2
Oct 21st, 2008
Hi Shubhang and welcome to DaniWeb

I am sorry, but I don't understand what you are asking. Can you please post some code to show us what you mean?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need help for making an address book using Java

 
0
  #3
Oct 21st, 2008
Originally Posted by darkagn View Post
Hi Shubhang and welcome to DaniWeb

I am sorry, but I don't understand what you are asking. Can you please post some code to show us what you mean?
I think he is saying that when the application is running, in DOS for example, the window becomes too crowded with all the System.out.println messages. So he would like a way to "clear" the command window from the messages.

But I don't know if there is a way to do it purely with java. Of course there are DOS and UNIX commands that do that, so IF there isn't another way, you might want to try and calling those from your program. But this I think will get a little more complicated than that you actually wanted.

Of course if there is another way feel free to correct me.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Re: Need help for making an address book using Java

 
0
  #4
Oct 21st, 2008
Originally Posted by javaAddict View Post
I think he is saying that when the application is running, in DOS for example, the window becomes too crowded with all the System.out.println messages. So he would like a way to "clear" the command window from the messages.

But I don't know if there is a way to do it purely with java. Of course there are DOS and UNIX commands that do that, so IF there isn't another way, you might want to try and calling those from your program. But this I think will get a little more complicated than that you actually wanted.

Of course if there is another way feel free to correct me.

In the program, I display the main menu,then ask for user's choice.
after the task is performed I display the main menu again to ask for what should be done next.

Each time the task is over it stays on the window and crowds it.
Is there any function or way to clear the command window before repeating the display of the main menu?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Need help for making an address book using Java

 
0
  #5
Oct 21st, 2008
clearing... not that I know of, but you can make a print of, for instance, thirty empty lines. this way, it 'll look as if the screen has been cleared. you might not have your menu on top of the screen every time though
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need help for making an address book using Java

 
0
  #6
Oct 21st, 2008
Actually I just thought of a VERY DUMP way to do it. It is not smart from programming terms.
I have already told you a way that might be done (running the appropriate DOS command using java) but if it is too difficult you can always try this:

  1. for (int i=0;i<100;i++) {
  2. System.out.println();
  3. }

It doesn't do what you want but it will look like it
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Re: Need help for making an address book using Java

 
0
  #7
Oct 21st, 2008
Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: guerrund is an unknown quantity at this point 
Solved Threads: 0
guerrund guerrund is offline Offline
Newbie Poster

help with java addressbook

 
0
  #8
Oct 7th, 2009
I am doing the same thing, but I need to know how to do the following:

Allow for editing

and

Allow for searching by state or by any other options

This is what I have so far:

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6.  
  7. public class AddressBook extends JFrame {
  8. // Specify the size of five string fields in the record
  9. final static int NAME_SIZE = 32;
  10. final static int STREET_SIZE = 32;
  11. final static int CITY_SIZE = 20;
  12. final static int STATE_SIZE = 2;
  13. final static int ZIP_SIZE = 5;
  14. // added record number
  15. final static int RECORDNUM_SIZE = 5;
  16. final static int RECORD_SIZE =
  17. (NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE + RECORDNUM_SIZE + ZIP_SIZE);
  18.  
  19. // Access address.dat using RandomAccessFile
  20. private RandomAccessFile raf;
  21.  
  22. // Text fields
  23. private JTextField jtfName = new JTextField(NAME_SIZE);
  24. private JTextField jtfStreet = new JTextField(STREET_SIZE);
  25. private JTextField jtfCity = new JTextField(CITY_SIZE);
  26. private JTextField jtfState = new JTextField(ZIP_SIZE);
  27. private JTextField jtfZip = new JTextField(ZIP_SIZE);
  28. private JTextField jtfRecordNum = new JTextField(RECORDNUM_SIZE);
  29.  
  30. // Buttons
  31. private JButton jbtAdd = new JButton("Add");
  32. private JButton jbtFirst = new JButton("First");
  33. private JButton jbtNext = new JButton("Next");
  34. private JButton jbtPrevious = new JButton("Previous");
  35. private JButton jbtLast = new JButton("Last");
  36. //edit button added
  37. private JButton jbtEdit = new JButton("Edit");
  38.  
  39. public AddressBook() {
  40. // Open or create a random access file
  41. try {
  42. raf = new RandomAccessFile("address.dat", "rw");
  43. }
  44. catch(IOException ex) {
  45. System.out.print("Error: " + ex);
  46. System.exit(0);
  47. }
  48.  
  49. // Panel p1 for holding labels Name, Street, and City
  50. JPanel p1 = new JPanel();
  51. p1.setLayout(new GridLayout(5, 1));
  52. p1.add(new JLabel("Name"));
  53. p1.add(new JLabel("Street"));
  54. p1.add(new JLabel("City"));
  55. // Record Number Label Holder
  56. p1.add(new JLabel("Record Number"));
  57. p1.add(new JLabel("Search: "));
  58.  
  59. // Panel jpState for holding state
  60. JPanel jpState = new JPanel();
  61. jpState.setLayout(new BorderLayout());
  62. jpState.add(new JLabel("State"), BorderLayout.WEST);
  63. jpState.add(jtfState, BorderLayout.CENTER);
  64.  
  65. // Panel jpZip for holding zip
  66. JPanel jpZip = new JPanel();
  67. jpZip.setLayout(new BorderLayout());
  68. jpZip.add(new JLabel("Zip"), BorderLayout.WEST);
  69. jpZip.add(jtfZip, BorderLayout.CENTER);
  70.  
  71. // Panel p2 for holding jpState and jpZip
  72. JPanel p2 = new JPanel();
  73. p2.setLayout(new BorderLayout());
  74. p2.add(jpState, BorderLayout.WEST);
  75. p2.add(jpZip, BorderLayout.CENTER);
  76.  
  77. // Panel p3 for holding jtfCity and p2
  78. JPanel p3 = new JPanel();
  79. p3.setLayout(new BorderLayout());
  80. p3.add(jtfCity, BorderLayout.CENTER);
  81. p3.add(p2, BorderLayout.EAST);
  82.  
  83. // Panel p4 for holding jtfName, jtfStreet, and p3
  84. JPanel p4 = new JPanel();
  85. p4.setLayout(new GridLayout(3, 1));
  86. p4.add(jtfName);
  87. p4.add(jtfStreet);
  88. p4.add(p3);
  89.  
  90. // Place p1 and p4 into jpAddress
  91. JPanel jpAddress = new JPanel(new BorderLayout());
  92. jpAddress.add(p1, BorderLayout.WEST);
  93. jpAddress.add(p4, BorderLayout.CENTER);
  94.  
  95. // Set the panel with line border
  96. jpAddress.setBorder(new BevelBorder(BevelBorder.RAISED));
  97.  
  98. // Add buttons to a panel
  99. JPanel jpButton = new JPanel();
  100. jpButton.add(jbtAdd);
  101. jpButton.add(jbtFirst);
  102. jpButton.add(jbtNext);
  103. jpButton.add(jbtPrevious);
  104. jpButton.add(jbtLast);
  105. // added
  106. jpButton.add(jbtEdit);
  107.  
  108. // Add jpAddress and jpButton to the frame
  109. add(jpAddress, BorderLayout.CENTER);
  110. add(jpButton, BorderLayout.SOUTH);
  111.  
  112. jbtAdd.addActionListener(new ActionListener() {
  113. public void actionPerformed(ActionEvent e) {
  114. String s;
  115. s = jtfName.getText();
  116. if (s.length() != 0) {
  117. writeAddress();
  118. jtfName.setText("");
  119. jtfStreet.setText("");
  120. jtfCity.setText("");
  121. jtfState.setText("");
  122. jtfZip.setText("");
  123. //added
  124. jtfRecordNum.setText("");
  125.  
  126. }
  127. }
  128. });
  129. jbtFirst.addActionListener(new ActionListener() {
  130. public void actionPerformed(ActionEvent e) {
  131. try {
  132. if (raf.length() > 0) readAddress(0);
  133. }
  134. catch (IOException ex) {
  135. ex.printStackTrace();
  136. }
  137. }
  138. });
  139. jbtNext.addActionListener(new ActionListener() {
  140. public void actionPerformed(ActionEvent e) {
  141. try {
  142. long currentPosition = raf.getFilePointer();
  143. if (currentPosition < raf.length())
  144. readAddress(currentPosition);
  145. }
  146. catch (IOException ex) {
  147. ex.printStackTrace();
  148. }
  149. }
  150. });
  151. jbtPrevious.addActionListener(new ActionListener() {
  152. public void actionPerformed(ActionEvent e) {
  153. try {
  154. long currentPosition = raf.getFilePointer();
  155. if (currentPosition - 2 * RECORD_SIZE > 0)
  156. // Why 2 * 2 * RECORD_SIZE? See the follow-up remarks
  157. readAddress(currentPosition - 2 * 2 * RECORD_SIZE);
  158. else
  159. readAddress(0);
  160. }
  161. catch (IOException ex) {
  162. ex.printStackTrace();
  163. }
  164. }
  165. });
  166. jbtLast.addActionListener(new ActionListener() {
  167. public void actionPerformed(ActionEvent e) {
  168. try {
  169. long lastPosition = raf.length();
  170. if (lastPosition > 0)
  171. // Why 2 * RECORD_SIZE? See the follow-up remarks
  172. readAddress(lastPosition - 2 * RECORD_SIZE);
  173. }
  174. catch (IOException ex) {
  175. ex.printStackTrace();
  176. }
  177. }
  178. });
  179.  
  180. // Display the first record if exists
  181. try {
  182. if (raf.length() > 0) readAddress(0);
  183. }
  184. catch (IOException ex) {
  185. ex.printStackTrace();
  186. }
  187. }
  188.  
  189. /** Write a record at the end of the file */
  190. public void writeAddress() {
  191. try {
  192. raf.seek(raf.length());
  193. FixedLengthStringIO.writeFixedLengthString(
  194. jtfName.getText(), NAME_SIZE, raf);
  195. FixedLengthStringIO.writeFixedLengthString(
  196. jtfStreet.getText(), STREET_SIZE, raf);
  197. FixedLengthStringIO.writeFixedLengthString(
  198. jtfCity.getText(), CITY_SIZE, raf);
  199. FixedLengthStringIO.writeFixedLengthString(
  200. jtfState.getText(), STATE_SIZE, raf);
  201. FixedLengthStringIO.writeFixedLengthString(
  202. jtfZip.getText(), ZIP_SIZE, raf);
  203. //added
  204. FixedLengthStringIO.writeFixedLengthString(
  205. jtfZip.getText(), RECORDNUM_SIZE, raf);
  206. }
  207. catch (IOException ex) {
  208. ex.printStackTrace();
  209. }
  210. }
  211.  
  212. /** Read a record at the specified position */
  213. public void readAddress(long position) throws IOException {
  214. raf.seek(position);
  215. String name = FixedLengthStringIO.readFixedLengthString(
  216. NAME_SIZE, raf);
  217. String street = FixedLengthStringIO.readFixedLengthString(
  218. STREET_SIZE, raf);
  219. String city = FixedLengthStringIO.readFixedLengthString(
  220. CITY_SIZE, raf);
  221. String state = FixedLengthStringIO.readFixedLengthString(
  222. STATE_SIZE, raf);
  223. String zip = FixedLengthStringIO.readFixedLengthString(
  224. ZIP_SIZE, raf);
  225.  
  226. jtfName.setText(name);
  227. jtfStreet.setText(street);
  228. jtfCity.setText(city);
  229. jtfState.setText(state);
  230. jtfZip.setText(zip);
  231. }
  232.  
  233. public static void main(String[] args) {
  234. AddressBook frame = new AddressBook();
  235. frame.pack();
  236. frame.setTitle("AddressBook");
  237. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  238. frame.setVisible(true);
  239. }
  240. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1345 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC