1

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.

7 Answers7

4

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.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
2

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");
            }
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
1

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.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

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

Waldheinz
  • 10,399
  • 3
  • 31
  • 61
0

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

jjmartinez
  • 779
  • 5
  • 20
  • 37
0

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

user2793390
  • 741
  • 7
  • 29
0

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