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!
Code :
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;
}
}
Re: Having trouble returning an array
Quote:
compiling error: incomplatible types
Please post the full text of the error message.
Where is the code that is trying to return an array?
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
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?
1 Attachment(s)
Re: Having trouble returning an array
Attachment 1356
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
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?
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:
Code :
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.
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();
}
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
Re: Having trouble returning an array
Quote:
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?
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.
Code :
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));
}
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"?
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)
Re: Having trouble returning an array
Quote:
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.
Re: Having trouble returning an array
All of the data are strings that are set in a different class.
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.
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.
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