Results 1 to 4 of 4
  1. #1
    navid is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default select 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?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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 :)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  4. #4
    navid is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot. I used the / and % and it worked for me.
    Thanks for your help.

Similar Threads

  1. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 03:49 PM
  2. Locating a digit within a string
    By ScentOfAWookie in forum New To Java
    Replies: 8
    Last Post: 03-27-2009, 06:23 PM
  3. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 08:23 AM
  4. Java newb Q: 8-digit id
    By 1111FM in forum New To Java
    Replies: 2
    Last Post: 09-29-2008, 03:37 PM
  5. convert getValue result in a 4 digit number
    By roseline43 in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •