Results 1 to 5 of 5

Thread: Index of an int

  1. #1
    wake is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Index of an int

    How would I access each index of an int.

    I can convert it to a String and then get the index but that gives a string. How would I leave the value indexed string as an int?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How would I access each index of an int.

    I can convert it to a String and then get the index but that gives a string. How would I leave the value indexed string as an int?
    Index of an int?

    Your question doesn't make much sense. Could you rephrase it? Also if you post some code that tries to do what you want, we may be able to understand it better.

  3. #3
    wake is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    I'll try again.

    I have a number, 12345, and want to work with each digit within the number. How could I go about adding 1+2+3+4+5?

    hope that makes more sense

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wake View Post
    I'll try again.

    I have a number, 12345, and want to work with each digit within the number. How could I go about adding 1+2+3+4+5?

    hope that makes more sense
    Thanks, that makes MUCH more sense.

    You could:
    A) convert it into a String (as you mentioned above), change it into a char array via the toCharArray() method and then iterate through the char array adding as you go. To convert the chars to the proper int, you could either convert them into a String and then use Integer.parseInt(theString) or do a little char math: int myInt = myChar - '0'; For example
    Java Code:
        char c = '4';
        int myInt = c - '0';
        System.out.println(myInt);
    Another possible solution is use integer division and mod and a while loop to extract each number and add them.

    There's also a recursive solution that can easily do this, but this may be overkill (or not).

    Good luck!

  5. #5
    wake is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Invalid Index
    By Allgorythm in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 01:51 PM
  2. Saving the index??
    By vividcooper in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 11:20 PM
  3. How to create an index for the
    By suneelakulkarni in forum Lucene
    Replies: 0
    Last Post: 07-13-2009, 04:23 PM
  4. I can't run my index.jsp
    By gissah in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 02:42 AM
  5. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM

Posting Permissions

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