basic java question (school easy)

RESmonkey

Diamond Member
May 6, 2007
4,818
2
0
How do I count the number of digits in an integer?

Say, for example, 4465. There are 4 digits.l How do I get Java to count that?

Thanks
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
There are many ways, but here's an easy one (pseudocode):

func num_digits(n):
. count = 0;
. while(n > 0):
. . count++;
. . n = floor(n / 10);
. end while
. return count
end func
 

stevf

Senior member
Jan 26, 2005
290
0
0
as esun mentioned there are lots of ways to do that. assuming this is for a class, do you have any requirements to follow? If so that should lead you to the methods they are looking for you to use. You could also cast the int to a string and then easily get the length of the string
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
For homework questions, you should always list the requirements and what you're "supposed" to know so far. You might get in trouble using language features not yet covered in class or ignoring the required method of solving the problem.

Also, if you don't want to get flamed you should list what (if any) effort you've made to solve the problem on your own.

I never answer "write it for me, me lazy!" questions from students, even though doing so will help them fail the class as karma says they deserve. For "I'm stuck on step 4 of 7, here's what I'm doing" questions I'll try to help.
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Convert it to a String and then use the length() method.

int someNumber = 1234;
String numString = someNumber + "";
int someNumbersLength = numString.length(); //should yield 4.

Tell me if I got it right when you get your homework assignment back.
 

Onund

Senior member
Jul 19, 2007
287
0
0
Originally posted by: ahurtt
Convert it to a String and then use the length() method.

int someNumber = 1234;
String numString = someNumber + "";
int someNumbersLength = numString.length(); //should yield 4.

Tell me if I got it right when you get your homework assignment back.

Java is really cool in that they provide wrappers for all their primitives with lots of functionality. My first thought was the same as this example but I would get the String a little differently:

String numString = Integer.toString(someNumber);

Or using a non-static functions:

Integer someNumber = new Integer(1234);
int someNumbersLength = someNumber.toString().length();
 

RESmonkey

Diamond Member
May 6, 2007
4,818
2
0
I think I'll do what ahurtt said and make it into a string, and then .length it. That's something we did cover.

Oh, and it's not my homework/I needed to know/etc. I'm supposed to write a program that takes a zip code, breaks it into separate digits, and replace the digits with certain code (what the postal service does). It reminded me of something I didn't know how to do on a previous test (how to count the digits). In this case, i didn't need to count them, because zip codes are 5 digits long. I just wanted to know for future reference.

Thanks for all your help guys