views:

308

answers:

7

Is there something I can use to see if a number starts with the same digit as another? Ex. 5643 and 5377 both start with five.

Is there a way to do this with numbers or do they have to be strings? (startsWith?)

+7  A: 

Ich n1 and n2 are your numbers:

return (n1.toString().charAt(0) == n2.toString().charAt(0));

You will have to use Integer not int, since int has no .toString() method.

If n1 is an int you could use new Integer(n1).toString().

Or even better (suggested by Bombe):

String.valueOf(n1).charAt(0) == String.valueOf(n2).charAt(0)
Burkhard
+1 That's using strings
Perchik
You could also use “String.valueOf(n1).charAt(0) == String.valueOf(n2).charAt(0)”. That way you can use whatever integral types you want. It would even work with doubles.
Bombe
Thanks. I change that :)
Burkhard
+1  A: 

Recurse dividing by ten until the whole division is less than ten, then that number is the first one and you can compare numbers.

Miquel
+1  A: 

You could calculate the first digit:

public int getFirstDigit(int number) {
    while (number >= 10) {
        number /= 10;
    }
    return number;
}

(This will only work for positive numbers.)

But then again you could just compare the first character of the String representations. The string comparison may be slower but as your problem is “getting the first character of the string representation” it might just be the appropriate solution. :)

Bombe
+1  A: 

The most straight-forward approach (allowing for different length values) would probably be just as you said, convert them both to Strings.

int x = 5643;
int y = 5377;
String sx, sy;

sx = "" + x;        // Converts int 5643 to String "5643"
sy = "" + y;

boolean match = (sx.charAt(0) == sy.charAt(0));
Bill the Lizard
I don't think you can do sx.[0] in Java. That smells like a C#-ism to me.
Paul Tomblin
@Paul: Thanks, I've been writing a lot of C++ lately. :)
Bill the Lizard
Downvotes are alot more effective if they're accompanied by comments.
Bill the Lizard
I didn't downvote, but now I'm tempted to just for "alot". :P
Michael Myers
@mmyers: D'oh! I just wasted a lot of time scanning my answer for the word "alot". I guess I need to spell-check my comments too. :)
Bill the Lizard
+1 Not sure why this is even downvoted since it's basically identical to the +7 accepted answer (all praise the new users page to see beyond 100 answers)
cletus
+1  A: 
  public static byte firstDigit(int number) {
    while (true) {
      int temp = number / 10;

      if (temp == 0) {
        return (byte) Math.abs(number);
      }

      number = temp;
    }
Andrey Vityuk
+1  A: 

(int)(a / 10 ** (int)Math.log10(a))

Probably less efficient than the string solution, but no looping and it keeps us in the "Numeric" realm.

(Always remember that when you are talking about the number of digits in a number, log10 is your friend)

Also won't work for negative numbers-take an absolute value of a first.

Bill K
A: 

For ints, int n1 and int n2;

return ("" + n1).charAt(0) == ("" + n2).charAt(0);
Chandan .
String.valueOf(n1) is clearer than a concatenation.
Nicolas
may be it is, but whats up with the negative? ""+n1 is one of ways to convert an int n1 into string and it works. I supposed downvote was for answers that are either wrong or that are not useful. And mine is not a wrong answer. A downvote may mislead others that ""+n1 doesn't work at all.
Chandan .