I'm writing a code for an assignment for school but it requires me to compare two int values. For example, if you have the number 123 and the other number is 321, they have the same digits but are in different orders. Is there easy way of comparing them or do i have to make them into a string and compare them as string types? if its the latter, how could i compare two strings? Is there any way of doing this without an array?
-
1What return-type and return-values will your method have? (How do you plan to express "they have the same digits but are in different orders" as a method result?) – ruakh Nov 05 '15 at 00:25
-
1My return type is void and im guessing it wants me to return a string. I'm doing an if-else statement in the body of the methods. The whole point of the project is to make a way to make a betting game where you input numbers and the type of game you want to bet. You win the most if the numbers are in the same order and you play the straight bet so i have if (bet type.equalsIgnorecase("Straight Bet") and if (player number == winning number) player wins, else they lose. Now i need to make an if statement for the when the numbers are out of order but the same, but im not sure how to – Rafael Leal-Mccormack Nov 05 '15 at 00:34
-
1So you're just looking to write a method of the form `boolean containSameDigits(int a, int b)` that checks if two integers contain the same digits (but perhaps in a different order)? – ruakh Nov 05 '15 at 00:36
-
1`My return type is void and im guessing it wants me to return a string.` OK, then... – TayTay Nov 05 '15 at 00:39
-
1yea but how could i do that? I was thinking if theres a way to compare strings, i could turn the int to a string and compare that string but i dont know if you could compare a string of numbers if they are out of order. – Rafael Leal-Mccormack Nov 05 '15 at 00:41
-
1the easiest and shortest code would probably be covert them to strings, sort the strings and see if the two strings are equal. Keep in mind this isn't the _fastest_ solution, just easiest to implement – jb. Nov 05 '15 at 01:15
3 Answers
By comparing int values, if you mean greater than, less than or equal you can do that like so.
int a = 123, b= 321;
if(a > b)
//a is greater than b (b is less than a)
if(a == b)
// a is equal to b
if(a < b)
// a is less than b (b is greater)
Could use some clarification, if you want to check if the number is reversed like you said in an example its called a palindrome.
You could reverse a number in the following if you had experience with loops and modulo(the %) in the following snippet.
int r = 0;
while(number != 0){
r = r * 10 + number % 10;
number /= 10; }
return r;
r would be that number reversed. If you input let's say 123 you would get 321 back, then you could compare it to the other to see if its just the reverse.
Let me know if you have any more questions and I'll try to answer!
To check if a number is arbitrarily mixed and not reversed to winning number, you could try the following.
Two numbers a and b, a is the winning number and b is the number the user chose. a is 251 and b is 521.
You could do this on each number to separate them.
int p1,p2,p3;
p1 = num % 10;
p2 = num / 10 % 10;
p3 = num / 100 % 10;
This would separate ex. 251 into 2, 5, and then 1. Then you could add them as so doing the same process for the second. sum is p1 + p2 + p3 and sum2 is p4 + p5 + p6 for the second number. Provided the numbers are not reversed. Use the thing I mentioned before for that case to check if they are flipped.
if(sum == sum2)
//Numbers are mixed but you won!
This should work.

- 71
- 3
- 12
-
1I see how that could help me but i need a way to compare numbers even if they arent reveresed, the assignment requires to compare 2 numbers so if the players number was 523 and the winning number was 253, (its arbitrarily mixed and not reversed) you would win, or 423 and 342. – Rafael Leal-Mccormack Nov 05 '15 at 00:46
-
1@RafaelLeal-Mccormack Check my answer. Think that should work. – Ricardo Rigaroni Nov 05 '15 at 00:57
-
1Awesome. Glad it helped. Do me a favor and accept my answer(click the check mark)! Happy coding. – Ricardo Rigaroni Nov 05 '15 at 01:00
Certainly not the fastest solution, but the code is short and easy to understand.
public boolean arePalindromes(int a, int b){
//convert them to char arrays for easy sorting
char[] aryA = String.valueOf(a).toCharArray();
char[] aryB = String.valueOf(b).toCharArray();
//sort them
Collections.sort(aryA);
Collections.sort(aryB);
//put them back to strings for easy comparison
String strA = new String(aryA);
String strB = new String(aryB);
//compare
return strA.equals(strB);
}

- 9,921
- 12
- 54
- 90
Please try the following code (I have tested it), of which idea is borrowed from here and here. This solution still uses array.
public class CompareInt {
public static void main(String[] args) {
System.out.println(containSameDigits(123, 123));
System.out.println(containSameDigits(123, 321));
System.out.println(containSameDigits(123, 132));
System.out.println(containSameDigits(123, 323));
System.out.println(containSameDigits(123, 124));
System.out.println(containSameDigits(123, 111));
}
public static boolean containSameDigits(int x, int y) {
String xSorted = getSortedString(x);
String ySorted = getSortedString(y);
return xSorted.equalsIgnoreCase(ySorted);
}
public static String getSortedString(int x) {
String xSorted = "";
for (int digit = 0; digit < 9; digit++) {
for (int temp = x; temp > 0; temp /= 10) {
if (temp % 10 == digit) {
xSorted += digit;
}
}
}
return xSorted;
}
}
Output:
true
true
true
false
false
false

- 1
- 1

- 1,171
- 6
- 16
- 21
-
To honor @ruakh I changed the function name to `containSameDigits` :-) – Tsung-Ting Kuo Nov 05 '15 at 00:40
-
1I would use an array, but the assignment is asking us to do it without an array and the professor hasnt taught arrays either. – Rafael Leal-Mccormack Nov 05 '15 at 00:42
-
The solution of @RicardoRigaroni is also elegant, but my solution can deal with the situation to compare 123 and 420 (which should return false). – Tsung-Ting Kuo Nov 05 '15 at 01:05
-
1You might want to update your method calls as well, because they still use `containSameDigits`. – Tom Nov 05 '15 at 01:10
-
1