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

How to convert a number into an Array

 
Nagaraju Pee
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can i convert a number into an array?

for ex: I have a number 34567.Now i want to convert that number into an array which has values like this
array[0]=3,array[1]=4,array[2]=5 and so on.

please help me at this context
 
D. Ogranos
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What format should your result array have? A String array, or an int array?

If you need a string array, then simply convert the number to a string and grab its characters with the charAt() method (or substring()) of the String class.

Else, you need to use divison by 10 repeatedly to find the digit values (for example, 34567 / 10000 = 3, then go on with the rest, 4567 / 1000 = 4, and so on).
 
Ravi Shankar Kumar
Ranch Hand
Posts: 30
1
Java MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi apply
this modulas logic

do {
arr[i] = number % 10;
number /= 10;
i++;
} while (number != 0);
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic