Why do you have to convert n to String again and again in the loop, and doing a substring for only one character? Can't you just store the string once in the very beginning of your program and just do charAt(i) - '0' in every loop?
No doubt it will work, though.
On 9/22/06, Mukul Gandhi <[EMAIL PROTECTED]> wrote:
This is a complete Java program doing this:
class X {
public static void main(String[] args) {
int n = 5732;
int len = ((new Integer(n)).toString()).length();
int[] array = new int[len];
for (int i = 0; i < len; i++) {
String x = ((new Integer(n)).toString()).substring(i, i + 1);
array[i] = (new Integer(x)).intValue();
}
}
}
Regards,
Mukul
Bullislander05 wrote:
> Hello,
>
> I'm fairly new to these parts and I come to ask a question.
>
> If anyone here has some experience in programming in Java, I would be
> enlightened to find out if there is any way to extract each digit from
> an integer.
>
> Say there was an integer with a value of 5732, how should I proceed to
> extract the 5, 7, 3, and 2 from said integer and transfer those values
> into an integer array?
>
> If anybody could help me out A.S.A.P I would be very grateful.
>
> Thank you,
> Bullislander05
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---
- [algogeeks] Obtaining each digit from an integer in Java. Bullislander05
- [algogeeks] Re: Obtaining each digit from an integer i... Vishal
- [algogeeks] Re: Obtaining each digit from an integ... Bullislander05
- [algogeeks] Re: Obtaining each digit from an integer i... Mukul Gandhi
- [algogeeks] Re: Obtaining each digit from an integ... Lego Haryanto