File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays

 
Julie Mendez
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a program that takes an integer number as input argument and returns the number of digits in that number. This is what I came up w/ so far. In my main program I ask them to enter some integers. And now i want to get the size of the integers they enter in so i can assign it to an array. (or can this only be used for javascript.

static int getSize(int num)
{
int count = 0;

return count = num.length;

} //ends getSize
[ October 25, 2004: Message edited by: Julie Mendez ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Julie-



num is an int, which is a primitive data type, not an array, so you can't check its length property because primitive data types don't have a length property.

If num were an array, you would only determine how many elements were in the array, not how many characters were in any of the array elements. But, an int doesn't work like a string -- it doesn't have a number of characters. If you have the user enter a number as a String, then you could use the length method of the String class to get the number of characters the user entered, in this case the number of digits in the number.
 
Jimmy Die
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Like mentioned before if the users entered them as a string then the .length() method works nicely. Or a recursive method can also work like below.
Cheers,

[ removed a recursive solution that used repeated division by ten, until no number greater than 9 was left, counting once for each division, plus once for any remaining digit -ds ]
[ October 26, 2004: Message edited by: Dirk Schreckmann ]
 
Priyaa V Kumar
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to convert the primitive data type, int to a String and then call the length() method...

[ code removed -ds ]

Hope this helps
[ October 26, 2004: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When helping folks to learn, let's prefer hints and nudges over coded solutions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic