Question:
How do I get individual numbers from another number in JAVA?
Akim
2011-09-19 04:41:01 UTC
For instance, I have the number 4967. I want to get the individual numbers (the 4, the 9, the 6, and the 7) out of that whole number, and save it in another variable. Example:

4967 contains a "4", a "9", a "6" and a "7". I want to save each one of those:

int number1 = 4;
int number2 = 9;
int number3 = 6;
int number4 = 7;

How do I do this? It doesn't have to be EXACTLY like I said. Any help is appreciated. Thanks in advance.
Five answers:
McFate
2011-09-19 04:52:15 UTC
You can use % (modulus) to strip off the last digit:



4967 % 10 = 7.



Then divide by 10 (to get 496) and repeat to strip off the 6, repeat again to get the 9, and repeat one last time to get the 4.



Something like this:



========

public ArrayList getDigits( int aNumber ) {

ArrayList retVal = new ArrayList();

while (aNumber != 0) {

retVal.add( 0, aNumber % 10 );

aNumber = aNumber / 10;

}

return retVal;

}

========



... if you want to feed in negative numbers, you'd have to handle that slightly differently (-1 % 10 = 9), but I'm assuming that's not necessary for this exercise.



My code is not incredibly efficient since it pushes onto the front of the array each time. Alternative approaches would include converting the number to a String and iterating over its characters.
I_M_Rock
2011-09-19 05:11:20 UTC
int n=1234;

int i=0;

int [] a=new int[4];



while(n>0)

{

a[i]=n%10; // stores the remainder after division

n=n/10; // removes the last digit after it is stored

i++;

}



for(i=a.length();a>=0;a--)

{

System.out.println(a[i]);

}





Write this code in your main class . Hope it works fine and u can understand it

make a few changes if required
roose
2016-12-01 12:49:03 UTC
Random variety subroutines intrinsic to languages which contain FORTRAN-ninety and C++ use algoithms to offer a chain of probably random numbers from 0 to a million. in spite of the fact that the set of rules demands a seed variety. you could think of designing a subroutine to calculate pi out to an arbitrary variety of digits, it would additionally p.c.. a undeniable component to digits after the decimal according to a seed variety you furnish. Random, interior the sense which you should no longer detect a development interior of that sequence, yet no longer random interior the sense which you nevertheless get the comparable sequence in case you supply it the comparable seed. in case you decide on an indefinitely long record of 'random' numbers you should run a application that continuously calculates the area of various 'planets' shifting interior the gravitational field of a equipment of three 'stars'. yet, lower back, in case you restart this technique with the comparable numbers you will get the comparable sequence. to eliminate the human component to repeatability, you should seed a random variety subroutine or the preliminary situations of a complicated actual equipment (one without an analytic strategies) with numbers desperate via variables we can be conscious yet no longer administration...case in point, the the maximum recent air temperature examining in Clovis, New Mexico, the sum of the magnitudes of the final 10 earthquakes mentioned via the U. S. Geological Survey...etc. Thats touching directly to the ultimate you're able to do i think of. i think of the pc courses used to p.c.. lottery numbers use a equipment akin to this - a ordinary random variety generator seeded via a series of observables autonomous of human administration.
Vaibhav
2011-09-19 05:41:21 UTC
Ok

let the number be in n

String s=String.valueOf(n);

int ar[] = new int[ s.length() ];

int i=0;

while(i
{

ar[i]=n%10;

n=n/10;

i++;

}

it also works for negative numbers
Kamal Nayan
2011-09-19 05:37:03 UTC
Thanx for d above answer. I have also asked a question similar to this bt havent got any answer.

Thanx once again..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...