| | |
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:
Solved Threads: 0
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.
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.
•
•
•
•
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?
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
•
•
Join Date: Oct 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
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?
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:
It doesn't do what you want but it will look like it
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:
Java Syntax (Toggle Plain Text)
for (int i=0;i<100;i++) { System.out.println(); }
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
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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:
Allow for editing
and
Allow for searching by state or by any other options
This is what I have so far:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class AddressBook extends JFrame { // Specify the size of five string fields in the record final static int NAME_SIZE = 32; final static int STREET_SIZE = 32; final static int CITY_SIZE = 20; final static int STATE_SIZE = 2; final static int ZIP_SIZE = 5; // added record number final static int RECORDNUM_SIZE = 5; final static int RECORD_SIZE = (NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE + RECORDNUM_SIZE + ZIP_SIZE); // Access address.dat using RandomAccessFile private RandomAccessFile raf; // Text fields private JTextField jtfName = new JTextField(NAME_SIZE); private JTextField jtfStreet = new JTextField(STREET_SIZE); private JTextField jtfCity = new JTextField(CITY_SIZE); private JTextField jtfState = new JTextField(ZIP_SIZE); private JTextField jtfZip = new JTextField(ZIP_SIZE); private JTextField jtfRecordNum = new JTextField(RECORDNUM_SIZE); // Buttons private JButton jbtAdd = new JButton("Add"); private JButton jbtFirst = new JButton("First"); private JButton jbtNext = new JButton("Next"); private JButton jbtPrevious = new JButton("Previous"); private JButton jbtLast = new JButton("Last"); //edit button added private JButton jbtEdit = new JButton("Edit"); public AddressBook() { // Open or create a random access file try { raf = new RandomAccessFile("address.dat", "rw"); } catch(IOException ex) { System.out.print("Error: " + ex); System.exit(0); } // Panel p1 for holding labels Name, Street, and City JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(5, 1)); p1.add(new JLabel("Name")); p1.add(new JLabel("Street")); p1.add(new JLabel("City")); // Record Number Label Holder p1.add(new JLabel("Record Number")); p1.add(new JLabel("Search: ")); // Panel jpState for holding state JPanel jpState = new JPanel(); jpState.setLayout(new BorderLayout()); jpState.add(new JLabel("State"), BorderLayout.WEST); jpState.add(jtfState, BorderLayout.CENTER); // Panel jpZip for holding zip JPanel jpZip = new JPanel(); jpZip.setLayout(new BorderLayout()); jpZip.add(new JLabel("Zip"), BorderLayout.WEST); jpZip.add(jtfZip, BorderLayout.CENTER); // Panel p2 for holding jpState and jpZip JPanel p2 = new JPanel(); p2.setLayout(new BorderLayout()); p2.add(jpState, BorderLayout.WEST); p2.add(jpZip, BorderLayout.CENTER); // Panel p3 for holding jtfCity and p2 JPanel p3 = new JPanel(); p3.setLayout(new BorderLayout()); p3.add(jtfCity, BorderLayout.CENTER); p3.add(p2, BorderLayout.EAST); // Panel p4 for holding jtfName, jtfStreet, and p3 JPanel p4 = new JPanel(); p4.setLayout(new GridLayout(3, 1)); p4.add(jtfName); p4.add(jtfStreet); p4.add(p3); // Place p1 and p4 into jpAddress JPanel jpAddress = new JPanel(new BorderLayout()); jpAddress.add(p1, BorderLayout.WEST); jpAddress.add(p4, BorderLayout.CENTER); // Set the panel with line border jpAddress.setBorder(new BevelBorder(BevelBorder.RAISED)); // Add buttons to a panel JPanel jpButton = new JPanel(); jpButton.add(jbtAdd); jpButton.add(jbtFirst); jpButton.add(jbtNext); jpButton.add(jbtPrevious); jpButton.add(jbtLast); // added jpButton.add(jbtEdit); // Add jpAddress and jpButton to the frame add(jpAddress, BorderLayout.CENTER); add(jpButton, BorderLayout.SOUTH); jbtAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s; s = jtfName.getText(); if (s.length() != 0) { writeAddress(); jtfName.setText(""); jtfStreet.setText(""); jtfCity.setText(""); jtfState.setText(""); jtfZip.setText(""); //added jtfRecordNum.setText(""); } } }); jbtFirst.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (raf.length() > 0) readAddress(0); } catch (IOException ex) { ex.printStackTrace(); } } }); jbtNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { long currentPosition = raf.getFilePointer(); if (currentPosition < raf.length()) readAddress(currentPosition); } catch (IOException ex) { ex.printStackTrace(); } } }); jbtPrevious.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { long currentPosition = raf.getFilePointer(); if (currentPosition - 2 * RECORD_SIZE > 0) // Why 2 * 2 * RECORD_SIZE? See the follow-up remarks readAddress(currentPosition - 2 * 2 * RECORD_SIZE); else readAddress(0); } catch (IOException ex) { ex.printStackTrace(); } } }); jbtLast.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { long lastPosition = raf.length(); if (lastPosition > 0) // Why 2 * RECORD_SIZE? See the follow-up remarks readAddress(lastPosition - 2 * RECORD_SIZE); } catch (IOException ex) { ex.printStackTrace(); } } }); // Display the first record if exists try { if (raf.length() > 0) readAddress(0); } catch (IOException ex) { ex.printStackTrace(); } } /** Write a record at the end of the file */ public void writeAddress() { try { raf.seek(raf.length()); FixedLengthStringIO.writeFixedLengthString( jtfName.getText(), NAME_SIZE, raf); FixedLengthStringIO.writeFixedLengthString( jtfStreet.getText(), STREET_SIZE, raf); FixedLengthStringIO.writeFixedLengthString( jtfCity.getText(), CITY_SIZE, raf); FixedLengthStringIO.writeFixedLengthString( jtfState.getText(), STATE_SIZE, raf); FixedLengthStringIO.writeFixedLengthString( jtfZip.getText(), ZIP_SIZE, raf); //added FixedLengthStringIO.writeFixedLengthString( jtfZip.getText(), RECORDNUM_SIZE, raf); } catch (IOException ex) { ex.printStackTrace(); } } /** Read a record at the specified position */ public void readAddress(long position) throws IOException { raf.seek(position); String name = FixedLengthStringIO.readFixedLengthString( NAME_SIZE, raf); String street = FixedLengthStringIO.readFixedLengthString( STREET_SIZE, raf); String city = FixedLengthStringIO.readFixedLengthString( CITY_SIZE, raf); String state = FixedLengthStringIO.readFixedLengthString( STATE_SIZE, raf); String zip = FixedLengthStringIO.readFixedLengthString( ZIP_SIZE, raf); jtfName.setText(name); jtfStreet.setText(street); jtfCity.setText(city); jtfState.setText(state); jtfZip.setText(zip); } public static void main(String[] args) { AddressBook frame = new AddressBook(); frame.pack(); frame.setTitle("AddressBook"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
![]() |
Similar Threads
- Any books a beginner should get? (Java)
- C++ forbids initialization of member (C++)
- Microsoft .NET FAQ (ASP.NET)
Other Threads in the Java Forum
- Previous Thread: 1 Error, program won't run.
- Next Thread: Making A Java Compiler... In Java
Views: 1345 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows







