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 17 of 17

Thread: Having trouble returning an array

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Having trouble returning an array

    Below is a my code, and i have got as far as creating the arrays, but im not too sure how to return them. I have made an effort but got the compiling error: incomplatible types. I have hilighted this code in ***Compiling error***

    Thanks in advance!

    import java.util.ArrayList;
     
    public class ContactList
    {
        private ArrayList<Contact> contactList;
     
        private String contactType;
     
        public ContactList()
        {
     
            contactList = new ArrayList<Contact>();
     
     
        }
     
        public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phoneNumber)
        {
            contactList.add(new AddPersonalContact(fName, sName, street, town, partOnePC, partTwoPC, phoneNumber));
     
     
        }
     
     
     
        public void addBusinessContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String eMail)
        {
     
            contactList.add(new AddBusinessContact(fName, sName, street, town, partOnePC, partTwoPC, eMail));
     
     
     
        }
     
         public String toString()
        {
            String output = "All Contacts:\n";
            for (ContactList contact : contactList)
            {
                output = output + account + "\n\n" ;
            }
            return output;
        }
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    compiling error: incomplatible types
    Please post the full text of the error message.

    Where is the code that is trying to return an array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    ]CODE]public String toString()
    {
    String output = "All Contacts:\n";
    for (ContactList contact : contactList)
    {
    output = output + account + "\n\n" ;
    }
    return output;
    } [/CODE]
    sorry, forgot to highlight the code, but incompatible types was the full compiling error

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    Please post the full text of the error message. It has the source code line number for the error.
    Also post the source code line causing the error or flag it in the posted source.

    What is the definition of the variable: account?

    Where is the code that is trying to return an array?
    Last edited by Norm; July 29th, 2012 at 11:31 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    errorjava.png

    and im not certain about the variable, i didnt really know how to return an array at all, but i have another project that compiles fine with it and i tried to convert it to use it in this program

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    You return an array by using the name of the array in a return statement:
    return theNameOfTheArray; // return array


    What does the variable: contactList contain?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Having trouble returning an array

    A few words... you truly ought to be using a StringBuilder here, rather than a String, first of all. Secondly, the error is because the "enhanced for loop" i.e. for-each construct syntax is (roughly - I'm purposefully not mentioning stuff like generic wildcards, primitive auto-(un)boxing, and arrays) as follows:
    for (element-type element-variable-name : instance-of-Iterable<element-type>) {
      ...
    }
    In other words, `contact` is meant to be a Contact, not a ContactList.
    Aside from that, you were attempting to use a variable named `account` in your loop, where you most likely meant `contact`. See below for corrected code.

    public String toString() {
      final StringBuilder output = new StringBuilder("All Contacts:\n");
      for (Contact contact : contactList) {
        output.append(contact).append("\n\n");
      }
      return output.toString();
    }
    Last edited by veeer; July 31st, 2012 at 01:26 PM.

  8. The Following User Says Thank You to veeer For This Useful Post:

    93tomh (July 30th, 2012)

  9. #8
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    Thank you very much! And can i ask you for the syntax to pull back data in the array that has either the "personal" or "business" contact type?
    Thanks again

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    the syntax to pull back data in the array
    Can you explain what you mean by "pull back"?
    Can you post an example array and explain what you want to do with the data in the array?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    I have 2 different contact types- Personal and Business. I need to return both business and personal contacts seperately. I dont know why i said pull back, i meant return, my mind must have gone blank.
    public class ContactList
    {
        private ArrayList<Contact> contactList;
     
        private String contactType;
     
        public ContactList()
        {
     
            contactList = new ArrayList<Contact>();
     
     
        }
     
        public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phoneNumber)
        {
            contactList.add(new AddPersonalContact(fName, sName, street, town, partOnePC, partTwoPC, phoneNumber));
     
     
        }
     
     
     
        public void addBusinessContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String eMail)
        {
     
            contactList.add(new AddBusinessContact(fName, sName, street, town, partOnePC, partTwoPC, eMail));
     
     
     
        }

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    Can you explain what problems you are having?
    By "types" do you mean class objects or are you talking about String data that has different content and is used in a different way.
    How do you determine when a String is a "business type" and when it's a "personal type"?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    I just dont know how to sort arrays by a certain string in them.

    So there are 2 different contact types (called contactType and is a String) These are automatically assigned when you call either the addPersonalContact or addBusinessContact to the appropriate type.

    So if i call the addPersonalContact method I would insert the name, address etc and "personal" will be automatically assigned to this data in the array list (in the form of a string)

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    how to sort arrays by a certain string in them.
    What kind of data is in the array? Strings or a class object that contains a String?
    The Arrays class has sort methods you can use, some with Comparators that you can write that will determine the order for the objects being sorted.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    All of the data are strings that are set in a different class.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    To sort class objects that are in an array, you could write a Comparator class for them and use the Arrays class's sort() method.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble returning an array

    Can you show me the syntax for a comparator class? Ive looked online for it and im not sure how to write this. Im not asking for you to write any code, but can you put it in sort of laymans terms please.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble returning an array

    A Comparator class implements the Comparator interface and defines the methods in the interface.
    There must be samples on the forum and net if you do a search.

    EDIT: Just found this in the tutorial:
    http://docs.oracle.com/javase/tutori...ces/order.html
    Last edited by Norm; July 30th, 2012 at 12:52 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  2. returning 2d array in java
    By dr.code.skm in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 10:14 AM
  3. Having trouble returning the right out put.
    By byako in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:49 PM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM