Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: address book

  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    Mountain View
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    address book

    I'm trying to get information from a user in order to create an address book. I'm just a beginner. I can't figure out what should I do to get the information saved and print the information to the console....right now I'm just trying to get the information and print it..but without succeed.


    import java.util.*;

    public class menu
    {
    public static void main(String[] args) throws Exception
    {
    Scanner scan = new Scanner(System.in);
    int menu = 0;

    System.out.println("*** book ***");
    System.out.println();
    System.out.println("1. Add a Personal Contact");
    System.out.println("2. Print the whole list of Contact");
    System.out.println("3. Search by Last Name");
    System.out.println("4. Exit");
    boolean quit = false;
    do
    {
    System.out.print("Please enter your choice: ");
    menu = scan.nextInt();
    System.out.println();
    switch(menu)
    {
    case 1:
    PersonInfo person = new PersonInfo();
    break;
    case 2:
    Print print = new Print();
    break;
    case 3:
    Search search = new Search();
    break;
    case 4:
    quit = true;
    break;
    default:
    System.out.println("Invalid Entry!");
    }
    }
    while (!quit);
    }
    }

    }
    }
    }
    import java.util.Scanner;
    import java.io.*;

    public class PersonInfo
    {
    private String name;
    private String lastName;
    private String address;
    private String email;
    private String phone;
    private String notes;

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    PersonInfo() throws IOException
    {
    System.out.println("Name: ");
    name = br.readLine();
    System.out.println("Last Name: ");
    lastName = br.readLine();
    System.out.println("Address: ");
    address = br.readLine();
    System.out.println("E-mail: ");
    email = br.readLine();
    System.out.println("Phone Number: ");
    phone = br.readLine();
    System.out.println("Add Note(s): ");
    notes = br.readLine();

    Contact contact = new Contact();
    }

    }


    public class Contact
    {
    private String name;
    private String lastName;
    private String address;
    private String email;
    private String phone;
    private String notes;

    public void setContact(String n, String ls, String ad, String e, String ph, String nt)
    {
    name = n;
    lastName = ls;
    address = ad;
    email = e;
    phone = ph;
    notes = nt;
    }
    public void printContact()
    {
    System.out.println("Name = " + name);
    System.out.println("Last Name = " + lastName);
    System.out.println("Address = " + address);
    System.out.println("E-mail = " + email);
    System.out.println("Phone = " + phone);
    System.out.println("Note(s) = " + notes);
    }

    }

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Hi. First, when posting code here, you should put your code inside the code tags. When posting, inside the editor, press the "#" button and you can post your code inside the code tags.

    It makes it MUCH easier for us to look at your code as right now, it is really hard to read.

    Also, where is your Person class? In your line when you are creating the PersonInfo class, you are also using a Person object, but I don't see your Person class code. Can you post it, too?

    Code:
    PersonInfo person = new PersonInfo();

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Hi Superteo, in the future could you please wrap your code in [code][/code] or [php][/php] tags in order to preserve the format?

    Lets take a look see.

    PHP Code:
    import java.util.*;

    public class 
    menu
    {
    public static 
    void main(String[] argsthrows Exception 
    {
       
    Scanner scan = new Scanner(System.in);
       
    int menu 0;

       
    System.out.println("*** book ***");
       
    System.out.println();
       
    System.out.println("1. Add a Personal Contact");
       
    System.out.println("2. Print the whole list of Contact");
       
    System.out.println("3. Search by Last Name");
       
    System.out.println("4. Exit");
    boolean quit false;
    do
       {
          
    System.out.print("Please enter your choice: ");
          
    menu scan.nextInt();
          
    System.out.println();
          switch(
    menu
          {
          case 
    1:
             
    PersonInfo person = new PersonInfo();
             break;
             case 
    2:
                Print print = new Print();
                break;
                case 
    3:
                   
    Search search = new Search();
                   break;
                   case 
    4:
     
    quit true;
    break;
    default:
    System.out.println("Invalid Entry!");
    }
    }
    while (!
    quit);
    }
    }

       }
    }
    }
    import java.util.Scanner;
    import java.io.*;

    public class 
    PersonInfo
    {
       private 
    String name;
       private 
    String lastName;
       private 
    String address;
       private 
    String email;
       private 
    String phone;
       private 
    String notes;
      
       
    InputStreamReader isr = new InputStreamReader(System.in);
       
    BufferedReader br = new BufferedReader(isr);
       
       
    PersonInfo() throws IOException
       
    {
          
    System.out.println("Name: ");
          
    name br.readLine();
          
    System.out.println("Last Name: ");
          
    lastName br.readLine();
          
    System.out.println("Address: ");
          
    address br.readLine();
          
    System.out.println("E-mail: ");
          
    email br.readLine();
          
    System.out.println("Phone Number: ");
          
    phone br.readLine();
          
    System.out.println("Add Note(s): ");
          
    notes br.readLine();
          
          
    Contact contact = new Contact();
       }
       
    }


    public class 
    Contact
    {
       private 
    String name;
       private 
    String lastName;
       private 
    String address;
       private 
    String email;
       private 
    String phone;
       private 
    String notes;
       
       public 
    void setContact(String nString lsString adString eString phString nt)
       {
          
    name n;
          
    lastName ls;
          
    address ad;
          
    email e;
          
    phone ph;
          
    notes nt;
       }
       public 
    void printContact()
       {
          
    System.out.println("Name = " name);
          
    System.out.println("Last Name = " lastName);
          
    System.out.println("Address = " address);
          
    System.out.println("E-mail = " email);
          
    System.out.println("Phone = " phone);
          
    System.out.println("Note(s) = " notes);
       }

       } 
    There is a couple of classes unavailable here, so I'm going to assume they don't have any relevance (the Print class and Search class).
    This seems to be more work than you really need, but I can see what you are doing here. A big part of the problem is simply that you are not collecting the Contact from PersonInfo anywhere. What I would do, since the PersonInfo doesn't appear to do anything other than collecting information through an input interface, is create a static method instead. Something like this:
    PHP Code:
    public class PersonInfo
    {
        public static 
    Contact createContact() throws NoSuchElementException
        
    {
            
    Scanner s = new Scanner(System.in); // I'm just going to use a scanner
            
    String namelastnameaddressemailphonenotes;
            
            
    name s.nextLine();
            
    lastName s.nextLine();
            
    address s.nextLine();
            
    email s.nextLine();
            
    phone s.nextLine();
            
    notes s.nextLine(); 
            
    s.close();
            return new 
    Contact(namelastNameaddressemailphonenotes);
        }

    Something like that. Note that there is no checking for datatypes or exceptions here.

    Next, I'd either change the setContact in Contact to be the constructor, or better to create a new constructor that chains directly to setContact so you can keep both. Usage is a matter of:
    PHP Code:
    try
    {
        
    Contact con PersonInfo.createContact();
        
    System.out.println("Contact information: " con);
    }
    catch (
    NoSuchElementException ex)
    {
        
    System.err.println("Unable to create a contact (scanner error: " ex.getMessage() . ")");

    Note the try/catch. As soon as you add to a definition of a method a throws, it then becomes what is called a 'checked' exception. This is required to be wrapped in a try/catch block (you may have seen this in compiler errors), or it will fail to compile. Leaving the 'throws' clause off of a signature will create an 'unchecked' exception which may throw a NoSuchElementException (or technically an IllegalStateException, but Scanner is having problems if it tosses that with the above code), but will not force you to actually try/catch it.

    Now to create this into an actual address book, you want to either use an array or a collection. I'd go for the collection as they are much easier to deal with than fighting with array scalings. Contact information sounds like something that is potentially sortable, can insert into the middle of a list, and remove from any location. For this, I'd use a collection called an LinkedList, or potentially a PriorityQueue the presents an algorithmic calculation to the names in order to auto-sort them. Alternatives are the Vector and ArrayList classes which are both scalable, but are not easy to remove items from the inner items in the lists (from an actual processing step). This can be as simple as:
    PHP Code:
    import java.util.LinkedList;
    public class 
    AddressBook extends LinkedList<Contact>
    {

    LinkedList is both a collection and a list, so it has methods such as add, remove, size, clear, toArray, and many others. These are then iterable for each 'Contact' in the list, and can be printed individually (looks like you're Print class is likely what is doing this). This would be like so:
    PHP Code:
    for (Contact c myAddressBookVariable)
    {
        
    System.out.println(c);

    For searching, you can either flatten this and use the Arrays class to search it, or you can iterate it. In order to do so, you'll want to override the .equals method on the Contact class to compare two contact for equality:
    PHP Code:
    public boolean equals(Object other)
    {
        
    boolean result false
        
    if (other instanceof Contact// whew, its been awhile, I think its instanceof that java likes.
        
    {
            
    result other.lastName.equals(this.lastName);
        }
        return 
    result;

    Hmm, but with multiple contacts under the same name this may not quite work that well.
    Oh, but the TreeSet collection actually has a subSet which returns a SortedSet<E> so you could technically splice out of a tree if you use a Comparable<E> interfaced object. This is starting to get a little more advanced; how much java experience did you have?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Oct 2010
    Location
    Mountain View
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    thanks

    I'm just a beginner...I will try to do few change and I will let you know...thanks for the moment

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Superteo1987 View Post
    I'm just a beginner...I will try to do few change and I will let you know...thanks for the moment
    For simplicity then I'd just use an iterable list and search it. Reference each of the items out that match your criteria and place them in a second list. You'll need to expose a method to fetch the last name off of the Contact, or to allow comparisons with equals / compareTo (via Comparable interface) to compare the two objects together and use a single parameter constructor for the last name to create a simple object to check.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    New Coder
    Join Date
    Oct 2010
    Location
    Mountain View
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    doesn't work why?

    Code:
    public class ContactList 
    {
       public ContactList()
       {
          Contact c;
          Contact[] array_list;
       }
    
          public void List(Contact size) 
          {
             Contact[] array_list = new Contact[1000];
             }
          
       public void print() 
       {
       
          int c;
          Object array_list;
          for (c = 0; c < array_list; c++) 
          {
             System.out.println(array_list[c]);
          }
       
       }
    
       public void NewContact()
       {
          
       }
       public void sort()
       {
          
       }
    
       }

  • #7
    New Coder
    Join Date
    Oct 2010
    Location
    Mountain View
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with save and searching

    I did some progress on my programs

    Now I can add a contact and print the list of all my contact. I was wondering if someone can give me any suggestions about how to save the contact to disk... and how to search in my address book by name.

    Thanks

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What are you currently using?
    Most of these collections implement an interface called Serlializable. This allows us to make use of the input and output objects as a stream, essentially flattening our collection to a readable and writable file with very minimal work on our side. If you are using an array, you'd need to move it to a different collection (even an arrayList is serializable).
    Here are a couple of links regarding the serialized input and output object streams:
    http://www.javapractices.com/topic/TopicAction.do?Id=57
    http://java.sun.com/developer/techni...serialization/

    If its still a primitive collection, you'll need to either promote it or wrap it in order to use the serialization (which is the easiest of course). If you don't want to do this, you can take an array and write the data to a file. The tricky part is when you are dealing with strings as you need to read in the size of the string in order to determine what its length is (handy when using random access interfaces).

    I'd go for the serialization.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •