Q: Comparing a string and an object property I'am trying to compare a string with an object property using .equals(), but the condition keeps failing even if its true.
I've tried using trim() to get rid off trailling spaces but still fails.
Heres my code:
if (status) {
        inventoryList = allInventory.getInventory();
        assert assetId != null;
        for (Inventory inventory : inventoryList) {
            if (assetId.trim().equals(inventory.getProductCode().trim())) {

                //get the current quantity that exists

                new_quantity.setValue(0);

                new_quantity.setMaxValue(inventory.getQuantity());
                inventoryId = inventory.getId();
                return;

            } else {
                // new_quantity.setVisibility(View.GONE);
                decrement.setEnabled(false);
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Product not found ");
                // prevent cancel of AlertDialog on click of back button and outside touch
                alert.setCancelable(false);
                alert.setMessage("Product is not in inventory. Do you want to continue scanning?");

                alert.setNegativeButton("No", (dialog, which) -> {
                            onBackPressed();
                            dialog.dismiss();
                        }
                );
                alert.setPositiveButton("Yes", (dialog, which) -> {
                    Intent scanAsset = new Intent(this, ScanActivity.class);
                    scanAsset.putExtra("building", building);
                    scanAsset.putExtra("room", room);
                    scanAsset.putExtra("department", depart);
                    scanAsset.putExtra("TAG", "E");
                    startActivity(scanAsset);
                    dialog.dismiss();
                });
                AlertDialog dialog = alert.create();
                dialog.show();
            }

        }


A: I would like to point out to the oracle docs, which says:

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.



A: try to use equalsIgnoreCase() instead of equals
