0

im trying to figure out how I can create an if statement that would be true if the the int provided matches any order inputted. For example, if the int is 745, make it so that 754 or 547 or 457 returns true. I was thinking turning into a string and then extracting each char value and adding the char values to make that the comparing value. WOuld that work? Sorry if question is dumb, Im new to java. Thanks in advance

UPDATE

I developed a code that works, except it works when input, for example, 647 and i compare it to 638 since they both have equal char value. how can I fix this?

public int winAmount()   

{        
 
 int prizeMoney = 0;
 String user = Integer.toString(userNumbers);
 String winning = Integer.toString(winningNumbers);
 int userValue = user.charAt(0)+user.charAt(1)+user.charAt(2);
 int winningValue = winning.charAt(0)+winning.charAt(1)+winning.charAt(2);
 
 if (betType.equalsIgnoreCase("Straight Bet"))      
  {
   if (userNumbers == winningNumbers)   
    
    prizeMoney = 600 * betAmount;
  
   else 
       
    prizeMoney = 0 ;
  }    
 
 else 
  {
   if (userValue==winningValue) 
   {
       if (user.charAt(0)==user.charAt(1) || user.charAt(0)==user.charAt(2) || user.charAt(1)==user.charAt(2))
   
      prizeMoney = 200 * betAmount;
       
   
       else
          
       
           prizeMoney = 100 * betAmount;
       
    }
  }    
     
    return prizeMoney; 
    
}   
   
}
  • So pretty much if that int contains a `4` and `7` and `5` in any order? Yeah just change to a string and do `.contains('7') && .contains(5) ......` – 3kings Mar 28 '16 at 22:21
  • 1
    *Adding* the chars wouldn't work, since you couldn't distinguish between `34` and `25`. – Andy Turner Mar 28 '16 at 22:22
  • *"WOuld that work"* It would, but would allow much more values, like `835`. What you want to do is this: [How to check if two words are anagrams](http://stackoverflow.com/q/15045640) (with numbers, but thats the same). – Tom Mar 28 '16 at 22:23
  • I mean, `7 * 5 * 4 = 140`, `5 * 4 * 7 = 140`, etc... – Matt Clark Mar 28 '16 at 22:31
  • 1
    @MattClark but, as a trivial counter-example, `7 * 5 * 2 * 2 = 140`, so your method would also match `7522` with `754`. – Andy Turner Mar 28 '16 at 22:50

2 Answers2

2

An elegant and easy way would be to sort your int by its digits. Then sort the other integers you want to compare with in the same way.

So, if you have 74442 when you will sort it you will have 24447.

Then if you compare with any other number like 47442 sorted it will be 24447.

Finally, if the sorted values are equal then that's it. (this will be a simple if statement)

The following is a very simple algorithm which will sort an integer by the digits.

All you have to do is to extract the algorithm and implement it in your own program which should be a piece of cake and quite efficient I think.

How to sort Integer digits in ascending order without Strings or Arrays?

Community
  • 1
  • 1
Theo
  • 1,165
  • 9
  • 14
0

the easiest way is to turn the integers to strings, order the the strings according to lexicographical order. and then test for equality.

public boolean isValid(int a, int b);   
{
    string s1 = String.valueOf(a);
    string s2 = String.valueOf(a);
    java.util.Arrays.sort(s1.toCharArray()); 
    java.util.Arrays.sort(ss.toCharArray()); 

    boolean isValid = s1.equale(s2);

    return isValid;

}
Alex
  • 159
  • 1
  • 9