如何在if语句中的integer.tostring上调用equalsignorecast [英] How do I invoke equalsignorecast on the integer.tostring in the if statement

查看:114
本文介绍了如何在if语句中的integer.tostring上调用equalsignorecast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的问题是,我试图将原语转换为整数而不是字符串,我已经设法使用integer.toString将字符串转换为int,但它继续在if语句中抛出此错误。不确定是什么问题。



< pre>无法在基本类型int上调用equalsIgnoreCase(int)





我尝试了什么:



我看过stackoverflow,我已经尝试了一切来解决问题,无法解决它可以有人帮助请。



无法在基本类型int  

这是我收到的错误信息。



错误行:

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







请参阅下面的代码:

 public static Film getNamedFilm(int id,String title,int year,String director,String stars,String评论){
Collection< Film> films = getSampleFilms()。values();
for(Film f:films){
// if(Integer.toString(f.getId() (id))&&
if(Integer.toString(f.getId()。equalsIgno reCase(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);
}

解决方案

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



您正在转换一些值(从 f.getId 返回)到一个字符串,然后尝试将其与整数值( id )进行比较但这没有任何意义,你无法将字符串与整数进行比较。但为什么要将一个值转换为字符串才能进行比较?为什么不写:

  if (f.getId()== id)&& 





你也标记了这个问题Javascript,但我有理由相信它是Java。


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);
}

解决方案

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:

if(f.getId() == id) &&



Also you have tagged this question Javascript, but I am reasonably sure it is Java.


这篇关于如何在if语句中的integer.tostring上调用equalsignorecast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆