0

My question is that there is a method printPetInfoByName() searches the list of pets for every pet of a given name, and prints the pet’s information, using the toString() method. The arraylist is of type Pet and name is given from main method (i haven't coded it yet) I need to print info of a pet using toString() by searching arraylist. If the given name is present in list then it will print info.

my code looks like thisenter image description here

but it produces error > incompatible types: Pet cannot be converted to CharSequence for line 20 if i write for(String search:list) then it gives error that Pet cannot be converted to String for line 18

6
  • 2
    contains doesn't know what to do with a Pet. You probably need to change it to something like it's name, making the line if(name.contains(search.getName())). Commented May 16, 2015 at 8:34
  • @Clashsoft if i write it that way then it states "cannot find symbol" for getName() Commented May 16, 2015 at 8:39
  • @Masu That's because you need to add the getName method in the Pet class to return the name of the Pet. Commented May 16, 2015 at 8:41
  • 1
    Please put your actual code in the question rather than a picture. Commented May 16, 2015 at 8:42
  • 1
    ok it got compiled after using getName(). Thanks but it prints hexadecimal address not the info when i input data there has to be a problem with toString method. but i dont know what to do Commented May 16, 2015 at 8:57

3 Answers 3

1

Use below code for your method:

public void printPetInfoByName(String name) {
    for (Pet search : list)
        if (search.getName().contains(name))
            search.toString(); //System.out.println(search.toString());
}

Define toString method in abstract class Pet.

Sign up to request clarification or add additional context in comments.

Comments

0

String.contains checks if a substring is present in the string. It takes a CharSequence as parameter, and you are now giving it a Pet. That why you get the error.

I assume you have some kind of name field on your pets that you can access with a get method? Instead of checking whether or not the string contains the pet, consider asking if the pet's name is equal to the search string.

I think you should get your way working first. But if you are interested java 8 streams are a good tool when working with Collections. Here is how one could solve your problem using them:

list.stream()
            .filter(p -> p.getName().equals(name))
            .forEach(System.out::println);

list.stream() "starts" the stream.

filter filters all elements p where p's name is equal to the name you pass as an argument.

forEach does something for all elements in the stream. Note that by filtering it, the remaining elements are those we want.

Comments

0

a String cant not compare with the complex object.They are not the same type,so your program doesn't know how to work with it .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.