Results 1 to 5 of 5
Thread: Index of an int
- 02-24-2010, 02:25 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
-
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?
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.
- 02-24-2010, 03:35 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
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
Another possible solution is use integer division and mod and a while loop to extract each number and add them.Java Code:char c = '4'; int myInt = c - '0'; System.out.println(myInt);
There's also a recursive solution that can easily do this, but this may be overkill (or not).
Good luck!
- 02-24-2010, 01:54 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Invalid Index
By Allgorythm in forum New To JavaReplies: 1Last Post: 02-16-2010, 12:51 PM -
Saving the index??
By vividcooper in forum New To JavaReplies: 1Last Post: 01-27-2010, 10:20 PM -
How to create an index for the
By suneelakulkarni in forum LuceneReplies: 0Last Post: 07-13-2009, 03:23 PM -
I can't run my index.jsp
By gissah in forum New To JavaReplies: 0Last Post: 03-23-2009, 01:42 AM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks