-1

I am trying to compare a variable but i want to consider the first digit. if item is 1 and I compare it with these values for AA {1,11,12} it will work in all of these values but I want to work just if AA = 1 and does not work with the rest . how can I do that. In the same time I want to deal with the AA as an integer.

                if (item == AA) {

                System.out.println("AA ");
            }
ibra
  • 1
  • `==` is usually done for **identity** comparison, not **equality**. Consider overriding `Object.equals` – Reut Sharabani Sep 06 '15 at 12:13
  • So you want a method like `boolean startWithSameDigit(int a, int b)`, is that right? How could do you check that in your mind or on paper? How could you translate that to code? – JB Nizet Sep 06 '15 at 12:15
  • @ReutSharabani What? Overriding `Object.equals()`... on a primitive, pre-existing type? – bcsb1001 Sep 06 '15 at 12:26
  • Look at the top answer for [this](http://stackoverflow.com/questions/2051817/return-first-digit-of-an-integer) question. Use that, then check for equality with `==`. – bcsb1001 Sep 06 '15 at 12:27
  • I didn't assume it's a primitive. I assumed it's a general question. With primitives obviously it's going to be different. – Reut Sharabani Sep 06 '15 at 12:28
  • @ReutSharabani "if item is 1..." Last time I checked, `1` is some integer type - all of which are primitives. Implementing your own number class is a huge waste of time. – bcsb1001 Sep 06 '15 at 12:29

1 Answers1

1

following will do it,if both item and AA are integers.

if(Integer.toString(item).charAt(0)==Integer.toString(AA).charAt(0))
Sumeet
  • 8,086
  • 3
  • 25
  • 45