-1

Okay, I'm totally rewriting this question because this works and I want to know why it works.

Suppose I have a number, testNumber with a value of 567.

I want to know if the next two numbers (shouldPassTest and shouldFailTest) the same digits, but in different 10s places.

So here's the code:

int testNumber = 567;
int num1 = 5;
int num2 = 6;
int num3 = 7;

int shouldPassTest = 756; 
int shouldFailTest = 777;


if(Integer.toString(shouldPassTest).matches("[5,6,7][5,6,7][5,6,7]")
{
    //Do some cool stuff
}

if(Integer.toString(shouldFailTest).matches("[5,6,7][5,6,7][5,6,7]")
    {
        //Do some cool stuff
    }

What happens when you run that, is that each digit is tested from the range of available digits (5, 6, and 7). Theoretically, shouldFailTest should actually pass the test seeing as how 7 matches one of my three criteria, albeit 3 times.

But what happens is that 777 returns false, when tested. This is precisely the result I wanted in my code, but I want to know why it happened. Does the matches method test to make sure that each number is only matched once?

Thanks!

This post was highly edited. After running my code, I found that the method does exactly what I want, but now I want to know why. Thanks.

  • It sounds like you're looking to see if one number is an anagram of another. – azurefrog Jan 30 '17 at 04:34
  • "Different solution". After using `Integer.toString`, break the strings down into characters, and use an array to hold the characters. Then you can remove stuff from the array. Even better would be to use a `Set`, but that won't work well if your numbers can have more than one of the same digit. But don't try to solve this with regexes. – ajb Jan 30 '17 at 04:36
  • Yes, and weirdly, the code does exactly what I wanted. 777 returns false. Now I'm both pleasantly surprised and confused. – Jonathan Martin Jan 30 '17 at 04:36
  • Actually, 777 doesn't return false. You are checking to see if Integer.toString(shouldFailTest).matches("[5,6,7][5,6,7][5,6,7]") returns true in your above code. – Edward Jezisek Jan 30 '17 at 04:47
  • The code you've excerpted won't even compile, because the parentheses aren't balanced in your `if` statement. – David Choweller Jan 30 '17 at 05:51

2 Answers2

1

I would use the following as regex is not a good solution to this problem:

public class Count {
    private int value;
    public Count() {
        value=0;
    }
    void increment() {
        value++;
    }
    void decrement() {
        value--;
    }

    public int getValue() {
        return value;
    }
}
public static boolean isAnagram(int val1, int val2) {
    Map<Character, Count> characterCountMap=new HashMap<>();
    for(char c:Integer.toString(val1).toCharArray()) {
        Count count=characterCountMap.get(c);
        if(count==null) { count=new Count(); characterCountMap.put(c, count);}
        count.increment();
    }
    for(char c:Integer.toString(val2).toCharArray()) {
        Count count=characterCountMap.get(c);
        if(count==null) { return false; }
        else { count.decrement(); }
        if(count.getValue()==0) {
            characterCountMap.remove(c);
        }
    }
    return characterCountMap.size()==0;
}

Please run:

System.out.println(Integer.toString(shouldFailTest).matches("[5,6,7][5,6,7][5,6,7]"));

to view the actual return value.

Edward Jezisek
  • 522
  • 4
  • 11
0

Theoretically, shouldFailTest should actually pass the test seeing as how 7 matches one of my three criteria, albeit 3 times.

But what happens is that 777 returns false, when tested. This is precisely the result I wanted in my code, but I want to know why it happened. Does the matches method test to make sure that each number is only matched once?

No, "777" does match the pattern you have specified "[5,6,7][5,6,7][5,6,7]"

Following condition in your code will evaluate to true.

if(Integer.toString(shouldFailTest).matches("[5,6,7][5,6,7][5,6,7]"))

VHS
  • 9,534
  • 3
  • 19
  • 43