Java Programmer
Java Programmer

Reputation: 205

If statement doesn't execute

I am creating a program that allows you to create a pie chart easily. In the method for removing a slice the if statement inside of a for loop doesnt execute and I cant figure out why this happens. Here is the removeSlice method:

public void removeSlice(Color color, float size, String displayText){
        int num = 0;
        System.out.println("Thing: " + color + " " + size + " " + displayText);
        for(int i = 0; i < slice.size(); i++){
            System.out.println("I: " + slice.get(i).color + " " + slice.get(i).size + " " + slice.get(i).text + " Current: " + i);
            if(slice.get(i).color == color && slice.get(i).size == size && slice.get(i).text.equals(displayText)){
                num = i;
                System.out.println("It Works");
            }
        }
        System.out.println(num);
        slice.remove(slice.get(num));
        totalSize -= size;
        --current;
    }

When trying to remove a slice the console output shows this

Thing: java.awt.Color[r=255,g=255,b=255] 100.0 Hey
I: java.awt.Color[r=0,g=0,b=0] 500.0 Hi Current: 0
I: java.awt.Color[r=255,g=153,b=153] 70.0 Hello Current: 1
I: java.awt.Color[r=255,g=255,b=255] 100.0 Hey Current: 2
I: java.awt.Color[r=153,g=153,b=0] 120.0 Hola Current: 3
0

as you see all of the values equal position 2's values in the ArrayList but still the if statement doesn't execute.

Upvotes: 1

Views: 147

Answers (7)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

You need to modify slice.get(i).color == color to slice.get(i).color.equals(color).

You should use .equals() method to compare color object.

if(slice.get(i).color.equals(color) && slice.get(i).size == size && slice.get(i).text.equals(displayText)){
                num = i;
                System.out.println("It Works");
            }

Upvotes: 2

jjmartinez
jjmartinez

Reputation: 807

try to compare the different values with "equals", no with "=="

Upvotes: 0

Michal V&#237;ch
Michal V&#237;ch

Reputation: 1

It's hard to say because we can't see whole source code but I think that your problem is here "slice.get(i).color == color". With == you tests if both variables reference the same object.

You should consider to use slice.get(i).color.equals(color) and you need to also implement equals, hashCode methods on that object

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

In addition to the == issue for Color, exact equality comparison for float can give problems. It will work if the values being compared were produced by exactly the same calculation, or if all calculations involved are exact. If not, there may be different rounding error leading to a very small difference in values that would be equal in real number arithmetic.

Small integer-valued floats such as 100.0 do represent the integer exactly, so that is probably not your current problem, but it could give you problems with different numbers.

Upvotes: 1

user2793390
user2793390

Reputation: 771

You need to use equals() instead of == for the Color objects.

Upvotes: 0

Waldheinz
Waldheinz

Reputation: 10487

As a Java Programmer, you should know about equals. Quite often this is what you really want.

Upvotes: 0

Luan Nico
Luan Nico

Reputation: 5917

You are comparing colors with ==. Use equals instead. == checks if the objects refer to the same place in memory. You create two colors, but with the same content - then, you must use equals to check if the content match.

Upvotes: 4

Related Questions