Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: phone book

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default phone book

    I'm creating a programs for a project with the propuse to create a simple address book...
    I have a class PersonInfo from where I get the information from the user...such as name, last name and so on.
    After I have a class contact...here I would like to save the contact inside and array...be able to print the all list of contact or just one...and ask to the user if he want to add a new one.....and in this class I have a bunch of probelms. I'm just a beginner. Someone can give me some idea about what i do wrong ?

    import java.awt.List;
    import java.util.ArrayList;
     
    public class Contact
    {
       String name, lastname, address, email, phone, notes;
       Contact()
       {
          ArrayList list;
          list = new ArrayList(10000);
       }
     
       Contact(int size) 
       {
          ArrayList list = new ArrayList(size);
     
       }
       void insert() 
       {
          List.add();
          }
     
       void view() 
       {
          for (int i = 0; i < list.size(); i++) 
          {
             ((PersonInfo)list.get(i)).printEntry();
             }
          }
     
       public Contact(String name, String lastname, String address, String email,
             String phone, String notes)
       {
          this.name = name; 
          this.lastname = lastname; 
          this.address = address; 
          this.email = email; 
          this.phone = phone; 
          this.notes = notes; 
       } 
       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); 
       } 
       }
    Last edited by copeg; November 29th, 2010 at 01:56 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: phone book

    Can you please define what you mean by 'bunch of problems'?

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    it doesn't compile.... if i take the array out...it works fine....i can enter one contact and print out the information of that contact....but when i start to work with the array ecc... the programs didn't work anymore

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: phone book

    For future reference, please post all compiler errors. Right now, it seems you are trying to use variables outside their scope. You create Lists in the constructors which I am guessing should be instance variables, right now the loose scope after those methods exit so trying to reference them in another method won't compile.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    sorry i didn't understand what should i do

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: phone book

    Create list as an instance variable...click on the link (blue text) in my post above.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: phone book

    What is with this method:
    void insert() 
       {
          List.add();
          }

    I would guess you are not wanting to reference the List Object here.

    Apart from creating list as an instance variable (like you did with the name, lastname, address, email, phone, and notes Strings) you also need to be careful of case when you reference your variables. Remember that JAVA is case sensitive and that will a bunch of problems if you are not careful.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    still....doesn't work....grgrgrgrgrg

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    i did
    inside class contact i delete all the part that i did with the array
    .....after the following method (print contact) i try to create a class contact list...in order to put the contact inside an array, print it and do other stuff... but i'm stuck it doesn't work

    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); 
       } 
     
       ContactList con = new ContactList();
     
       }
     
    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()
       {
     
       }
     
       }
    Last edited by copeg; November 29th, 2010 at 05:44 PM.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: phone book

    For future reference, please use the code tags (read the signature of aussiemcgr for details on how to use the highlight tags).

    Once again, even if you have, re-read the link I posted above, because it contains information in there that you are missing. The code you posted is still creating local instances which lose scope after the method exits...
    public void list(Contact size){
             Contact[] array_list = new Contact[1000];
    }
    ...creates a new Contact array, which looses scope after the method exits, with the instance variable still not instantiated...
    public void list(Contact size){
             array_list = new Contact[1000];
    }
    ...instantiates the instance variable array_list as an array of type Contact and size 1000 (the values it contains still have yet to be instantiated however). Redefining the variable in the method just creates a local variable and does not use the instance variable

  11. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    still...I think i did the change u suggested but it doesn't work

  12. #12
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    ummm...I don't get it...I'm trying to follow your suggestion but it doesn't work can u be more precise

  13. #13
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: phone book

    ummm...I don't get it...I'm trying to follow your suggestion but it doesn't work can u be more precise

  14. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: phone book

    Define 'doesn't work', and please post any updated code you have thus far.

  15. #15
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: phone book

    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); 
       } 
     
       ContactList con = new ContactList();
     
       }
     
    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()
       {
     
       }
     
       }

    First things first: Is there a main somewhere? It would be easier to see all your code because the way its written itll just make contact c and your contact array and exit

    2)
    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); 
       } 
     
       ContactList con = new ContactList();
     
       }

    I spy two many close brackets? it seems possible

    public void List(Contact size) 
          {
             Contact[] array_list = new Contact[1000];
             }

    I'm thinking you don't mean to pass a Contact object named size?? I think you want to pass an int that you would put where the 1000 is.


       public void print() 
       {
     
          int c;
          Object array_list;
          for (c = 0; c < array_list; c++) 
          {
             System.out.println(array_list[c]);
          }
     
       }

    i don't think that println will work. or atleast they way you think it will. You would want to use your printContact method and pass the contact array_list[c] to it. Then do array_list[c].name etc...

    If you need more help, you can pm me. i threw together a quick program that'll do what i think your trying to do and i can give you more pieces of that if you need help.

Similar Threads

  1. How to create a database in my cell phone?
    By Viggopiano in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: July 12th, 2010, 07:26 AM
  2. useing the comp's phone jack
    By wolfgar in forum Java Theory & Questions
    Replies: 10
    Last Post: November 7th, 2009, 03:57 PM
  3. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 11:15 AM
  4. hey - Mobile phone drinking game
    By pazzy in forum Java ME (Mobile Edition)
    Replies: 9
    Last Post: August 4th, 2009, 09:41 AM
  5. [SOLVED] How to start writing java mobile application?
    By Koâk in forum Java ME (Mobile Edition)
    Replies: 15
    Last Post: July 30th, 2009, 01:52 AM