Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find digit in number

Status
Not open for further replies.

keizersoz

Programmer
Apr 22, 2004
67
0
0
BE
Hi,

I need to find out in a certain digit appears in an int. (for instance the digit 2 appears in the int 582).

Does someone know some useful functions that can help me solve that problem. The only way I see to solve this is look at the length of the int and then by % getting all the digits. But it seems to me to complicated for such an easy assignment.

Any help is welcome. Thanks a lot.

Janssens
 
first convert the int value to string, then use the equals method in the String class to compare your value to the string itself.
 
That would only work if the digit you're looking for is the same than the whole number.

I'd go stefan's way.

Btw, I'm curious about the module thingie. How would yo use it? Moduling 10, 100, 100, etc?

Cheers,
Dian
 
Code:
public boolean isDigitInInt (int digit, int value)
{
        while (value > 0)
        {
                if ((value % 10) == digit) return true; 
                value /= 10; 
        }
        return false;
}
a bit more work is to do, if value might be negativ.


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top