Reputation: 1
I have to write a program where I have to store clients number address and id I created arrayList for all one of the options is that the program ask for the id number and gives the information if it doesnt exist display not found
Here's what I have wrote for searching for the client Id
public static List<Integer> findClient (List<Integer> idnumbers, Integer searchterm )
{
List<Integer> result = new ArrayList<Integer>();
for(Integer idnum : idnumbers)
{
if(idnum.contains(searchterm))
result.add(idnum);
}
return result;
}
I get red lines on the .contains saying "The method contains (Integer) is undefined for the type Integer"
Any help how can I fix that?
Upvotes: 0
Views: 294
Reputation: 993
Although you got write answer but a few thing you can change to meet your requirement.
You have to store client number, address and id but in arraylist you are just saving id.
better you should make a class for client like this
class Client{
int id;
String address;
String number;
//getters and setters
}
Now store client information in arraylist of type Client like this -
ArrayList<Client> clients = new ArrayList<Client>();
and then to search a particular client you have to way -
1 - through directly checking id of each client in arraylist like this -
for(Client client:clients){
if(client.getId==idToSearch)
return client;
}
2 - override equals method of your client class in a way that it will check if id of the client is same then they are same. You can use your ide to generate equals method then directly use contains mehtod like this
//Make a constructor with id for client class
Client client = new client(idToSearch);
if(clients.contains(client)){
clients.get(clients.indexOf(client));
}
I will prefer 2nd way hope it helps in your code design.
Upvotes: 0
Reputation: 2189
if(idnumbers.contains(searchterm))
Can't call contains on an Integer. It's only one number...
Upvotes: 0
Reputation: 9946
contains is a method on Collection so you can use it on your array list but not on Integer
for your case, you need to use equals method on Integer. here is the modified method:
public static List<Integer> findClient (List<Integer> idnumbers, Integer searchterm )
{
List<Integer> result = new ArrayList<Integer>();
for(Integer idnum : idnumbers)
{
if(idnum.equals(searchterm))
result.add(idnum);
}
return result;
}
Upvotes: 2