简体   繁体   English

二进制运算符'!='的错误操作数类型第一类型:字符串第二类型:int

[英]bad operand types for binary operator '!=' first type: String second type: int

I have a problem concerning the display , I would like to see each items from my variable CodeA of type String which is in a JComboBox. 我有一个关于显示的问题,我想从JComboBox中的String类型的变量CodeA中查看每个项目。 The variable CodeA represents each identification from the table Album. 变量CodeA代表表Album中的每个标识。

In my DaoAlbumMySql I have error message.. 在我的DaoAlbumMySql中,我收到错误消息。

bad operand types for binary operator '!=' first type: String second type: int 二进制运算符'!='的错误操作数类型第一类型:字符串第二类型:int

The problem is according NetBeans the if (idCat != 0) 问题是根据NetBeans的if (idCat != 0)

   public ArrayList <Album> selectAlbums (String idCat)
        {
            ArrayList <Album> myList = new ArrayList();
            String req;

            if (idCat != 0)
            {
                req = "Select CodeA, TitreA, A.IdentC, DenomCat, " +
                " DateArrivee from album A,  " +
                "chanteur C where A.IdentC = C.IdentC" +
                " and CodeA = " + idCat + " order by 1";

            }

Two problems here: 这里有两个问题:

1) You are trying to compare a String to an int ( 0 ). 1)您正在尝试将String与int0 )进行比较。 You need to put quotation marks around it to make it a String to String comparison. 您需要在其周围加上引号,以使其成为“ StringString比较。

2) You are comparing with != . 2)您正在与!=进行比较。 This is incorrect to compare Strings. 比较字符串是不正确的。 Compare with the .equals() method when comparing Strings 比较Strings时与.equals()方法进行比较

(As it is you are checking for (With != ) reference comparison (address comparison) The .equals() method checks for content comparison.) (因为您正在检查(使用!= )引用比较(地址比较), .equals()方法检查内容比较。)

Unsure what you really want do do. 不确定您真正想要做什么。 For the moment you compare an String to an Integer. 目前,您将字符串与整数进行比较。

If idCat should not be null then you have to 如果idCat不应该为null,则必须

if (idCat != null)

If it must not null nor empty 如果它不能为null也不为

if ((idCat != null) && (!idCat.isEmpty())

or last if it should not contain the value "0" 或最后一个,如果它不应该包含值“ 0”

if (!"0".equals(idCat))

Two comments: 两条评论:

1) You can't compare String with int in Java. 1)您不能在Java中将String与int进行比较。 I think you tried to detect a null value? 我认为您尝试检测空值? Depending the version of Java you're using, you could use java.lang.Optional as an alternative to others provided answers 根据您使用的Java版本,可以使用java.lang.Optional作为其他人提供的答案的替代方法

2) Try to use interface instead of implementation classes when it is possible... Here, you can replace a lot of ArrayList to List. 2)尝试尽可能使用接口而不是实现类...在这里,您可以将很多ArrayList替换为List。

Regards 问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制运算符“-”的错误操作数类型第一类型:int; 第二种类型:java.lang.String - bad operand types for binary operator “-” first type: int; second type: java.lang.String 二进制运算符&#39;/&#39;的错误操作数类型第一类型字符串第二类型int - Bad Operand types for binary operator '/' first type String second type int 二元运算符&#39;|的操作数类型错误 |” 第一种类型:int; 第二种类型:int。 这是什么意思? - Bad operand types for binary operator '| |' first type: int; second type: int. What does this mean? 如何修复错误“二元运算符 &#39;&gt;=&#39; 第一种类型的错误操作数类型:int[] 第二种类型 int” - How to fix the error "Bad Operand Types for Binary Operator '>=' first type: int[] second type int" 二元运算符“&amp;”的错误操作数类型第一种类型:int[] 第二种类型:int - Bad operand types for binary operator ‘&’ First type: int[] Second type: int 二元运算符“!=”的错误操作数类型 第一个类型 int[] 第二个类型 int - Bad operand types for binary operator “!=” first type int[] second type int 获取二元运算符&#39;&gt;&#39;的错误错误操作数类型第一种类型:double []第二种类型:int - Getting error bad operand types for binary operator '>' first type: double [] second type: int 错误:二元运算符'.='的错误操作数类型 if(Current_value:= ":"){ ^ 第一种类型:char 第二种类型:String - error: bad operand types for binary operator '!=' if(Current_value != "."){ ^ first type: char second type: String 二进制运算符的错误操作数类型+第一类型int []和第二类型int - Bad operand type for binary operator + first type int[] and second type int 为什么我会收到此错误? “二元运算符“&amp;&amp;”的操作数类型错误 第一种类型:int 第二种类型:布尔值 - Why am I getting this error? "Bad operand type for binary operator "&&" first type: int second type: boolean"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM