• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Hod do you compare int values?

 
Ranch Hand
Posts: 263
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 39399
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparing primitives uses ==
 
Saloon Keeper
Posts: 6049
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Sorry, StringBuilder would be good than String since String is immutable.
 
Carey Brown
Saloon Keeper
Posts: 6049
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 263
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 6049
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the digit in the one's position
 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the int to String with toString() or your choice
Then separate the numbers and compare them with ease
 
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12754
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 6049
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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) ?
 
Saloon Keeper
Posts: 3415
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For simple cases like this I use String s = "" + 3.
 
Bartender
Posts: 2330
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 13569
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!