Reputation: 437
How can I make comparisons to the Object class?
I tried
if ("0.0".equals(price) ||"0.0".equals(quantity))
and
if: (price.equals("0.0") ||quantity.equals("0.0"))
In the last else it always prints 0.0
but nothing happen with if!!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){for (int i = model1.getRowCount() - 1; i >= 0; i--) {
Object price = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 8);
Object quantity = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 11);
//if ("0.0".equals(price) ||"0.0".equals(quantity))
if (price.equals("0.0") ||quantity.equals("0.0"))
{
model1.removeRow(i);
System.out.println("price&: "+price);
System.out.println("quantity&: "+quantity);
}else{
System.out.println("price: "+price);
System.out.println("quantity: "+quantity);
}
}
}
Upvotes: 1
Views: 98
Reputation: 437
when i try:
if(object1 == null || object2 == null) {
model1.removeRows();
}
it works but only when the object is null and not content "0.0" so finally i convert the objects to Strings and here is what i did:
private void btn_clearActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = model1.getRowCount() - 1; i >= 0; i--) {
Object price = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 8);
Object quantity = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 11);
String s1=price.toString();
String s2=quantity.toString();
//if ("0.0".equals(price) ||"0.0".equals(quantity))
if (s1.equals("0.0") ||s2.equals("0.0"))
{
model1.removeRow(i);
}
}
}
Upvotes: 1
Reputation: 12615
Object
, String
and Double
classes all have a .equals()
method, but they work differently.
For Object
class comparison is made to the Object's hash code (the unique object ID) for equality. That comparison asks "Are these the same object?"
For String class, .equals()
does a string comparison, which is what I think you want, whether you're comparing String
objects containing strings or string literals, or a combination of the two. Inside the JVM actually uses string interning
where only one copy of a string's value is stored for the whole JVM, regardless of which object (or literal representation) refers to it, but that is invisible to you and doesn't matter for what you're doing... String.equals()
compares string values and that's all you need to know.
For Double
class the comparison is numeric.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = model1.getRowCount() - 1; i >= 0; i--) {
Object price = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 8);
Object quantity = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 11);
if (price instanceof String && quantity instanceof String) {
if (((String)price).equals("0.0") || ((String)quantity).equals("0.0")) {
model1.removeRow(i);
System.out.println("price&: "+price);
System.out.println("quantity&: "+quantity);
} else {
System.out.println("price: "+price);
System.out.println("quantity: "+quantity);
}
}
}
}
The above is probably what you want, assuming model1.getValueAt()
is returning a String type. If it isn't guaranteed to return a specific type then you'll need check the type of object it returns, before assigning or testing the value, to make sure it is downcast to the proper type.
But that's another subject.
Upvotes: 1
Reputation: 5862
In Java, calling equals on an object uses the object's identity. Unless the calls to getQuantity and getPrice return Strings, they will not equal. I would suggest that you not use raw Objects and use the actual types for your instances, override equals if you need to do things like compare Strings to Doubles, etc.
Upvotes: 2