I still think making your integer a String and then using toCharArray() is both cleaner and more understandable:
int fullInt = 1234567890;
char[] chars = (Integer.toString(fullInt)).toCharArray();
for(int i = 0; i < chars.length; i++) {
int answer = Character.getNumericValue(chars[i]);
System.out.println(">>>>>answer =" + answer);
}
The whole point of Java IMHO is you do not need to write 50 lines of docs for every method.
:-)
Greg
-----Original Message-----
From: Nathan Tenney [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, September 04, 2002 8:21 AM
To: JDJList
Subject: [jdjlist] Re: Getting digits from an integer
As a side note, Integer arithmetic in Java doesn't
round, it truncates. So, this could be accomplished
more simply just by dividing by a power of 10. For
example, if your number was 123, and you wanted the
first digit, you would divide by 100 (10^2). To get
the 2, you would divide by 10 (10^1), and to get the
3, you would divide by 1 (10^0). So quite simply, a
function like this one would suffice.
//This function assumes that pos starts at zero (the
right most
//digit) and goes to n-1, where n is the number of
digits in the
//number.
int getDigit(int pos, int num){
//It is ok to typecase Math.pow here because we
are dealing
//with integers only.
int digitpos = (int)Math.pow(10,pos);
if(num > digitpos)
return num/digitpos;
else //There is no number at this position
return -1;
}
--- Jeff Fisher <[EMAIL PROTECTED]> wrote:
> Thanks, never thought about the simple way.
>
> -----Original Message-----
> From: Joseph Ottinger [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, September 04, 2002 8:12 AM
> To: JDJList
> Subject: [jdjlist] Re: Getting digits from an
> integer
>
>
> You could always try simple math and simple logic.
> Try neatoriffic stuff
> like this:
>
> public int getFirstDigit(int value) {
> int rounded=(value/10)*10;
> int result=value-rounded;
> return result;
> }
>
> >From: Jeff Fisher <[EMAIL PROTECTED]>
> >Reply-To: "JDJList" <[EMAIL PROTECTED]>
> >To: "JDJList" <[EMAIL PROTECTED]>
> >Subject: [jdjlist] Getting digits from an integer
> >Date: Wed, 4 Sep 2002 07:59:02 -0400
> >
> >I need to be able to get each individual digit from
> an integer as an
> >integer. I think I should be able to do this by
> using the >> operator, but
> >I just can't seem to figure it out.
> >
> >For example if I have a number 123, I need to get
> 3, then the 2, then the
> >1.
> >It doesn't really matter the order, I just need the
> digits. I suppose I
> >could convert it to a string and go that route, but
> that seems like a
> >waste.
> >
> >Does anyone have experience with this or can point
> me to some
> >examples/information.
> >
> >Thanks
> >
> >Jeff
> >
> >To change your JDJList options, please visit:
> >http://www.sys-con.com/java/list.cfm
>
>
>
>
>
_________________________________________________________________
> MSN Photos is the easiest way to share and print
> your photos:
> http://photos.msn.com/support/worldwide.aspx
>
>
> To change your JDJList options, please visit:
> http://www.sys-con.com/java/list.cfm
>
> To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
=====
----------------------------------
Nathan Tenney
Alumni Utah State University
[EMAIL PROTECTED]
----------------------------------
__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm