0

I am trying to check whether a user input value is present in an ArrayList or not but I kept getting this error.

cannot convert int to String

Here is my code:

System.out.println("Enter receiver's name: ");

String receiversearch=input.nextLine();
for(Contact contactmain:phonebook) {
    if(contactmain.getfname().contains(receiversearch)==true){
        receiversearch=phonebook.indexOf(phonebook);

    }

    else {
        System.out.println("Contact not found");
    }
}
blurfus
  • 13,485
  • 8
  • 55
  • 61
  • Does this answer your question? [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – M. Dudek Sep 21 '21 at 05:52
  • you'll need to parse the int. String myValue = intValue; will give you that error, you'll need either: String myValue = "" + intValue; (String concatenation) or String myValue = Integer.toString(intValue); or similar – Stultuske Sep 21 '21 at 05:53
  • @Dudek he's trying the other way around – Stultuske Sep 21 '21 at 05:54
  • Hi, this line seems wrong: `phonebook.indexOf(phonebook)` . Moreover in which line do you get the error? – pleft Sep 21 '21 at 05:56
  • @pleft that is likely the line that causes the error, since he's trying to assign the result to a String – Stultuske Sep 21 '21 at 06:01
  • @Stultuske not only this, but he tries to get the `indexOf(phonebook)` where phonebook is a `List` object and not a `Contact` object which are the list's items. – pleft Sep 21 '21 at 06:02
  • `String receiversearch` is declared as String. Later, in `receiversearch = phonebook.indexOf(phonebook);`, you try to assign an int to it. That cannot work. Make up your mind which type your variables should have :) or use distinct variables – knittl Sep 21 '21 at 06:03
  • @pleft ehm ... so? I don't see your point there – Stultuske Sep 21 '21 at 06:27
  • @pleft Thank you very much. It worked. By the way I'm new in the field of java, there are some things i do not understand yet, anyway thank you very much for helping me guys. – Bradley Rose Sep 21 '21 at 06:52

1 Answers1

0

Here is a re-write of your code, maybe it can help you

    System.out.println("Enter receiver's name: ");

    String receiversearch=input.nextLine();
    Contact foundContact = null; // here we will store the found contact (if any)
    for(Contact contactmain:phonebook) {
        if (contactmain.getfname().contains(receiversearch)) { // == true is redundant, contains(..) returns a boolean
            //receiversearch=phonebook.indexOf(phonebook);  //this is wrong and not needed, you can remove this line. Your phonebook items are Contact objects and not String so you cannot assign them to String. Moreover indexOf(phonebook) is wrong, you are trying to get the index of a List instead of an item on the List
            foundContact = contactmain;     //contact is found assign it to foundContact
            break;      //break the for loop since we found what we were looking for
        }
    }

    if (foundContact != null) { 
        System.out.println("Contact has been found!"); // foundContact is not null which means that we have found it
    } else {
        System.out.println("Contact not found"); // foundContact is null which means that we couldn't find it
    }
pleft
  • 7,567
  • 2
  • 21
  • 45
  • you are just altering functionality "assuming" something isn't needed there. If he wants the phonenumber, that is needed. – Stultuske Sep 21 '21 at 06:29
  • @Stultuske I am not altering functionality, as OP states in his question: *I am trying to check whether a user input value is present in an ArrayList*. If he finds the correct Contact object in his list, then he can retrieve any property/value from it, including the "phonenumber" (btw what phonenumber???) or anything else this Contact object has. `phonebook.indexOf(phonebook)` is wrong, he asks a List for the index number of the List itself, I cannot see how this works. – pleft Sep 21 '21 at 06:36
  • @Stultuske so, may i ask you to explain why we need the receiversearch=phonebook.indexOf(phonebook) and how is this line is gonna give us the phonenumber (either as a string or int) ? All he needs is the Contact. – Melron Sep 21 '21 at 06:52
  • I admit the code there doesn't make sense, the original code shows that he wants to return the phonenumber of the contact (that is what you change). I can easily add a name in there, without adding the phone number, so in your code, it would just return 'found it' without checking whether or not there is a phone number. Now, there's a lot off in his code (either that, or he's not showing the code that actually throws the exception), I'm not saying that, but you are making assumptions and altering the functionality – Stultuske Sep 21 '21 at 07:03
  • @Stultuske `the original code shows that he wants to return the phonenumber of the contact` where did you see this thing? I can see ***fname*** – Melron Sep 21 '21 at 07:11
  • @Stultuske, I do not share the same view as yours, however lets let the OP decide if my answer is helpful or not and lets let him comment if I am altering the functionality. His code excerpt is not helping either to understand what exactly he is trying to achieve. But nor me nor you are in his mind so lets let him speak for himself (and his code) :) – pleft Sep 21 '21 at 07:11