Hello every one,

I want to select a digit in a number. for example select 3 in 1234.

Does anyone know how can I do that?

Printable View

- 12-09-2010, 08:23 PMnavidselect a digit in a number
Hello every one,

I want to select a digit in a number. for example select 3 in 1234.

Does anyone know how can I do that? - 12-09-2010, 08:30 PMeRaaaa
String.valueOf(1234) and then charAt or toCharArray and array[2] or with mathematics a/=10 + a%10 or the scanner class or ...many many more :)

- 12-09-2010, 09:03 PMpbrockway2Quote:

I want to select a digit in a number. for example select 3 in 1234.

Are you trying to find the character (digit) given its index (eRaaaa's examples)? Or trying to find the index given the digit? For the latter there is the String method indexOf().

-------------

It's worth nothing that these sorts of problem are intrinsically about strings (/numerals/string representations of numeric quantities) rather than numbers. The / and % approach, as given, depends on the digit being sought in a base 10 representation: but, of course, Java ints (like numeric quantities generally) have no base. - 12-12-2010, 10:47 AMnavid
Thanks a lot. I used the / and % and it worked for me.

Thanks for your help.