• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert a number into an Array

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 34
1
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi apply
this modulas logic

do {
arr[i] = number % 10;
number /= 10;
i++;
} while (number != 0);
reply
    Bookmark Topic Watch Topic
  • New Topic