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.

Page 8 of 9 FirstFirst ... 6789 LastLast
Results 176 to 200 of 209

Thread: Address Book Program Issues

  1. #176
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Sorry it's tiredness and my punctuation is going to pot.
    Correction - making silly mistakes it's fine now it will display the list

    public void viewEntries(){
                String forename = "";
                String surname = "";
                String street = "";
                String city = "";
                String county = "";
                String country = "";
                String post = "";
                  if(addressbook.isEmpty()){
                    System.out.println("Address Book is empty");
                    return;
                }
                Name name = new Name(forename, surname);
                Postcode postcode = new Postcode(post);
                Address address = new Address(street, city, county, country);
                Contact contact = new Contact(name, address);
                addressbook.viewContact(contact);
     
           }
    Last edited by Gamb1t; August 25th, 2011 at 07:54 PM.

  2. #177
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Though i seem to have an issue.
    After entering 2 contacts and then requesting to view the list i got this printed for some strange reason

    Street Address: townsend croft, City: coventry, County: west midlands, Country united kingdom, Postcode: null
    Forename: philip, Surname: stone

    1 Postcode:Contact@70453807
    Street Address: townsend croft, City: coventry, County: west midlands, Country united kingdom, Postcode: null
    Forename: philip, Surname: stone

    2 Forename:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 Surname:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 Street:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 City:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 County:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 Country:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

    2 Postcode:Contact@5bcdbf6
    Street Address: weston gardens, City: isleworth, County: london, Country united kingdom, Postcode: null
    Forename: richard, Surname: stone

  3. #178
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    Your program appears to be in a loop, repeatedly printing out the last entry in the list.
    Look at your logic to see why the code in the loop does not move the next item in the list or exit the loop.

    Why do you create new objects in the viewEntries method? All the entries to be viewed should be in the list.

  4. #179
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    It seemed the only way to get it to compile.
    I require this - addressbook.viewContact(contact); so it links to addressbook and performs the function.
    And it says it can't find the variable contact.
    It ends up going back to having

    Name name = new Name(forename, surname);
    Postcode postcode = new Postcode(post);
    Address address = new Address(street, city, county, country);
    Contact contact = new Contact(name, address);

    and then it can't find the variables its what i have in my addcontact method

    As for the loop in my AddressBook i am using

    public void viewContact(Contact contact)
            {
                 for( int index = 0; index < top; index++)
                    {
                        System.out.println((index+1)+" Forename:"+
                        list[index].toString());
                        System.out.println((index+1)+" Surname:"+
                        list[index].toString());
                        System.out.println((index+1)+" Street:"+
                        list[index].toString());
                        System.out.println((index+1)+" City:"+
                        list[index].toString());
                        System.out.println((index+1)+" County:"+
                        list[index].toString());
                        System.out.println((index+1)+" Country:"+
                        list[index].toString());
                        System.out.println((index+1)+" Postcode:"+
                        list[index].toString());
     
                    }
            }

  5. #180
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    I should use a for each loop shouldn't i, so for each of the data values in an index use toString and enter the value. And will need an arraylist then i imagine - it's something like this isn't it -
    new ArrayList<Contact>Arrays.asList(array);

    meh this to finish, delete method and search method to do and know doubt find things won't work after that think i'm scuppered
    Last edited by Gamb1t; August 25th, 2011 at 08:27 PM.

  6. #181
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Address Book Program Issues

    for (Contact c : list){
    System.out.println(c.toString());
    }

    That should work with an Array, so no need to use an ArrayList.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. The Following User Says Thank You to newbie For This Useful Post:

    Gamb1t (August 25th, 2011)

  8. #182
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    So
     for (Contact index : list)
                 {
                     System.out.println(index.toString());
                 }

    This says for each Contact index in list then system prints out the index and the toString method. wait it can't be index that would print out the number not the data

  9. #183
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    What class is the list variable?
    What does that class's to String method return?
    In the code you posted, you call the toString method seven times for each index's value.

  10. #184
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    The list variable is in the class AddressBook set as an array
    private Contact[] list;

    The toString method comes from Contact class
    @Override  
        public String toString() {  
            return super.toString() + "\n" + address + "\n" + name + "\n";  
        }

    returning the name and address variables

  11. #185
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    So i only need to call it once as it takes care of all the variables?
    And do i then need to override it and have it with the AddressBook class too

    God i'm getting frustrated and i'm not gonna get this finished
    Last edited by Gamb1t; August 25th, 2011 at 08:48 PM.

  12. #186
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    You need to think about what data you want to print and where that data is located.
    What is in the list array?
    If you get an object from the list array, what does it contain? How do you get the pieces of data you want to print from the object that you get from the array?

  13. #187
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    The data i want to print is all the variables from name and all the variables from address.
    All of which is found under
    Name name = new Name(forename, surname);
                Postcode postcode = new Postcode(post);
                Address address = new Address(street, city, county, country);
                            Contact contact = new Contact(name, address);

    My list array contains - list[top] = contact; so the index number basically
    If i get an object from it, it contains the index number and contact which contains the name and address variables

  14. #188
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    You need to design what is to go on each line that you print out.
    Then you need to figure out where each piece of data on that line comes from.
    Then you can write the code to print out that line with all those pieces of data.

  15. #189
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    So i need
    for (Contact top : list)
                 {
                     System.out.println(top.toString());
                 }
    ??

  16. #190
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    try it an see what happens.

  17. #191
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    It compiles but i get an error message when i use the method - "java.lang.NullPointerException: null"
    error message points System.out.println(top.toString());
    It's seems to show everything but it gives null for the postcode

  18. #192
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    Please show the FULL output.

  19. #193
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Contact@3bc0f2e5
    Street Address: west, City: south, County: east, Country north, Postcode: null
    Forename: phil, Surname: stone

    Contact@788ab708
    Street Address: south , City: east, County: north, Country west, Postcode: null
    Forename: rich, Surname: smith


    "java.lang.NullPointerException
    at AddressBook.viewContact(AddressBook.java:49)
    at AddressTextUi.viewEntries(AddressTextUi.java:123)
    at AddressTextUi.displayMainMenu(AddressTextUi.java:4 6)
    at AddressTextUi.mainMenu(AddressTextUi.java:14)
    at AddressbookDriver.main(AddressbookDriver.java:10)"

  20. #194
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    Look at this simple program segment. If the array contains null, null is returned by the for each:
          String[] list = new String[3];  // create with 3 slots
          list[0] = "test";                     //only put element in one
          for(String s : list) {                // read all slots including emptys
            System.out.println("s=" + s);
          }
    /*  The print out from the above
    s=test
    s=null
    s=null
    */

    Your code must test for a null value before using it.

  21. #195
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Okay that makes sense but where and how do i do this? Sorry my brain has melted

  22. #196
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    Don't use the for each loop. You have a variable with the number of contacts in the list. Use a normal loop with the value of that variable controlling the number of loops.

  23. #197
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Is this something different to the null thing?
    so my variable is list[top]
    so i need to use the top and that sets the amount of loops,
    i really can't think anymore

  24. #198
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    What value is in the top variable?
    Is it the index of the last element in the list or is it the count of the number of items in the list?

  25. #199
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    I have these

    //index of the last entry
    private int top = 0;
    //constant number that indicates the maximum
    //number of entries in the address book
    private static final int MAXENTRIES = 5;
    //array of Address Book Entries
    private Contact[] list;

  26. #200
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Address Book Program Issues

    I don't think you answered my question.
    What value is in the top variable?
    Is it the index of the last element in the list or is it the count of the number of items in the list?

    Or is it the index for the next item to be added to the list. This would often be the same as the number of items in the list

Page 8 of 9 FirstFirst ... 6789 LastLast

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 12:49 PM
  2. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 07:23 PM
  3. Address Book Help
    By clevel211 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 21st, 2010, 09:42 PM
  4. How to get your computer name and IP address?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 6
    Last Post: November 11th, 2010, 05:06 AM
  5. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 04:08 AM