This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert a number into an Array

 
Nagaraju Pee
Greenhorn
Posts: 5
  • 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
  • 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
  • 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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic