Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem which I got is, I am trying to convert primitive into to integer instead of string and I have managed to convert the string to int using integer.toString but it keeps throwing this error on the if statement. Not sure what the problem is.

<pre>Cannot invoke equalsIgnoreCase(int) on the primitive type int


What I have tried:

I have looked on stackoverflow and I have tried everything to solve the problem, cant manage to solve it can someone help please.

Cannot invoke equalsIgnoreCase(int) on the primitive type int

This is the error message which I am getting.

Error Line:
if(Integer.toString(f.getId().equalsIgnoreCase(id)) &&



See the code below:
public static Film getNamedFilm(int id, String title, int year, String director, String stars, String review) {
    Collection<Film> films = getSampleFilms().values();
    for(Film f: films) {
        //          if(Integer.toString(f.getId()(id)) &&
        if(Integer.toString(f.getId().equalsIgnoreCase(id)) &&
                (f.getTitle().equalsIgnoreCase(title)) &&
                //                  Integer.toString(f.getYear().equalsIgnoreCase(year)) &&
                Integer.toString(f.getYear()) &&
                (f.getDirector().equalsIgnoreCase(director)) &&
                (f.getStars().equalsIgnoreCase(stars)) &&
                (f.getReview().equalsIgnoreCase(review))) {
            return(f);
        }
    }
    return(null);
}
Posted
Updated 15-Dec-18 22:00pm

1 solution

Java
if(Integer.toString(f.getId().equalsIgnoreCase(id)) &&

You are converting some value (returned from f.getId) to a string, and then trying to compare it with an integer value (id). But that makes no sense, you cannot compare a string with an integer. But why are you converting a value to a string in order to compare it? Why not just write:
Java
if(f.getId() == id) &&


Also you have tagged this question Javascript, but I am reasonably sure it is Java.
 
Share this answer
 
Comments
Dhanyaal 16-Dec-18 9:16am    
Hi, the line which you have sent me is wrong, it throws back syntax error:Syntax error on token "&&", throw expected

With this line;
if(f.getId() == id) &&
Richard MacCutchan 16-Dec-18 10:32am    
Sorry, it should be
if(f.getId() == id &&
Dhanyaal 16-Dec-18 13:28pm    
What about this line;
(f.getYear()==year) &&


if(f.getId() == id &&
(f.getTitle().equalsIgnoreCase(title)) &&
(f.getYear()==year) &&
// Integer.toString(f.getYear()) &&
(f.getDirector()==(director)) &&
(f.getStars().equalsIgnoreCase(stars)) &&
(f.getReview().equalsIgnoreCase(review))) {
return(f);
}
Richard MacCutchan 17-Dec-18 4:13am    
What do you mean? Does it work or not?
Dhanyaal 17-Dec-18 10:05am    
It doesn't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900