简体   繁体   中英

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. The variable CodeA represents each identification from the table Album.

In my DaoAlbumMySql I have error message..

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

The problem is according NetBeans the 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 ). You need to put quotation marks around it to make it a String to String comparison.

2) You are comparing with != . This is incorrect to compare Strings. Compare with the .equals() method when comparing Strings

(As it is you are checking for (With != ) reference comparison (address comparison) The .equals() method checks for content comparison.)

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

if (idCat != null)

If it must not null nor empty

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

or last if it should not contain the value "0"

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

Two comments:

1) You can't compare String with int in Java. 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

2) Try to use interface instead of implementation classes when it is possible... Here, you can replace a lot of ArrayList to List.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM