Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# Hod do you compare int values?

Ranch Hand
Posts: 289
2
if i had an int variable

and another one

how could i compare if there is the same number in each, like the 2?

author & internet detective
Posts: 40035
809
Comparing primitives uses ==

Bartender
Posts: 7196
65

Bartender
Posts: 1248
86

wayne brandon wrote:how could i compare if there is the same number in each, like the 2?

I guess you mean to say, how to compare two numbers to find out same digits present in those two numbers. For example num1 = 1028 and num2 = 2205 Output: 0 2

In that case first you need to separate each digits of num1 to compare with every digits of num2. I think you better use String rather than int type for numbers since number such as 0235 will create problem while separating digits using % and / operators.

See String for charAt(..) and other String methods which might help.

Ganesh Patekar
Bartender
Posts: 1248
86

Ganesh Patekar wrote:... you better use String....

Sorry, StringBuilder would be good than String since String is immutable.

Carey Brown
Bartender
Posts: 7196
65

Ganesh Patekar wrote:I think you better use String rather than int type for numbers since number such as 0235 will create problem while separating digits using % and / operators.

That would depend on the requirements. If they say two int's then leading zero's don't exist. If they say two Strings representing a number then it may or may not allow leading zeros. At this point, without further refinement of the requirements, I would assume that it involves two int's. In that case you can use '/' and '%' to separate the digits.

wayne brandon
Ranch Hand
Posts: 289
2
Hi Carey

you can use '/' and '%' to separate the digits.

is this purely a mathematical thing? my maths is pretty poor, how would you use divide and % to separate numbers? why do the numbers even have to be separated? is there no method
just to compare to digits?

Carey Brown
Bartender
Posts: 7196
65
To get the digit in the one's position

Ganesh Patekar
Bartender
Posts: 1248
86

Carey Brown wrote:... If they say two int's then leading zero's don't exist. If they say two Strings representing a number then it may or may not allow leading zeros...

Ahh I see, agreed.

Greenhorn
Posts: 1
Change the int to String with toString() or your choice
Then separate the numbers and compare them with ease

Marshal
Posts: 69740
277
Welcome to the Ranch

Ranch Meyo wrote:Change the int to String with toString() . . .

. . . but an int doesn't have a toString() method. It doesn't have any methods. You need something else to create such a String: this method might be the best way to do it.

lowercase baba
Posts: 12869
62

wayne brandon wrote:how could i compare if there is the same number in each, like the 2?

Just a tip...programmers tend to be extremely literal.  When you say 'int vale', programmers think about the full value of the number.  What you really want to do is find the set of digits in each of two numbers, and find the intersection.

One of the hardest jobs of coding is coming up with clear, well defined, and complete specs.  Writing code is about 90% thinking, and only 10% typing.

Carey Brown
Bartender
Posts: 7196
65

Campbell Ritchie wrote:Welcome to the Ranch

Ranch Meyo wrote:Change the int to String with toString() . . .

. . . but an int doesn't have a toString() method. It doesn't have any methods. You need something else to create such a String: this method might be the best way to do it.

What's the difference between String.valueOf(i) and Integer.toString(i) ?

Bartender
Posts: 3993
156
For simple cases like this I use String s = "" + 3.

Saloon Keeper
Posts: 2610
126
Just wanted to point out, if the number has a negative value, then the modulus operator(%) would give you a negative value for every iteration. This may not be what OP is looking for.

@OP, the modulus operator (%) gives the division remainder. This works well for +ve numbers %10 since when dividing by 10, the last digit is always the remainder and that's why n % 10 gives you the last digit. Another use for % is when checking for even and odd numbers using 2 as divisor.

Next, the division (/) works in the above case because of loss of precison. If we have a number (say 203) and we divide it by 10, it would return 20.3. But, since we are using int to store it's value, the 0.3 is ignored and the result is just 20. This effectively shortens the number by a digit.

I think that Strings are best suited for this kind of requirement. Assuming that you're only interested if the digit exists in the second number, then String's contains method is good, else you can you indexOf method. However, you might want to consider handing numbers beginning with a 0 first (octal numbers).

Sheriff
Posts: 15733
264

wayne brandon wrote:
how could i compare if there is the same number in each, like the 2?

Just curious but what's the point of this exercise?  Also, can you clarify your intent? Do you want to check if two numbers have similar digits in the same positions (e.g. hundreds/tens/ones)? Or do you just want to check if two numbers have the same digit in any location, like how 200 and 123 both have the digit 2?

Campbell Ritchie
Marshal
Posts: 69740
277

Carey Brown wrote:. . . What's the difference between String.valueOf(i) and Integer.toString(i) ?

Hardly anything. But what I meant is, you can't write

Campbell Ritchie
Marshal
Posts: 69740
277
Actually, there is a difference; String#valueOf() is overloaded and can take absolutely every type, reference and primitive, and will accept nulls.You can't unbox null; you will have a null pointer exception thrown. You can also apply a radix to the Integer method.

 Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad! Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature