Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 119,729 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,329 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Separate a five digit number into parts

 
Reply to this topicStart new topic

Separate a five digit number into parts

junaidjee
post 26 Jun, 2008 - 05:58 AM
Post #1


New D.I.C Head

*
Joined: 6 Sep, 2007
Posts: 19


My Contributions


Suppose i have a five digit number. How can i convert this number so that this number is divided into five parts and then separated by three blank spaces.
Suppose i have a number 12345 then i want output like that 1 2 3 4 5
User is offlineProfile CardPM

Go to the top of the page


AmitTheInfinity
post 26 Jun, 2008 - 06:07 AM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 868



Thanked 24 times

Dream Kudos: 125
My Contributions


QUOTE(junaidjee @ 26 Jun, 2008 - 06:28 PM) *

Suppose i have a five digit number. How can i convert this number so that this number is divided into five parts and then separated by three blank spaces.
Suppose i have a number 12345 then i want output like that 1 2 3 4 5



use mod 10 div 10 method.

java


int i=12345,rem;

while(i>0)
{
rem = i % 10;
i /=10;
System.out.print(rem);
System.out.print(" ");
}


I hope this will help you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

lordms12
post 28 Jun, 2008 - 05:34 AM
Post #3


D.I.C Regular

Group Icon
Joined: 16 Feb, 2008
Posts: 301



Thanked 12 times

Dream Kudos: 225
My Contributions


You can simply convert this integer value to string then process the string as you want.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 28 Jun, 2008 - 06:13 AM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,368



Thanked 136 times

Dream Kudos: 75
My Contributions


QUOTE(AmitTheInfinity @ 26 Jun, 2008 - 06:07 AM) *

QUOTE(junaidjee @ 26 Jun, 2008 - 06:28 PM) *

Suppose i have a five digit number. How can i convert this number so that this number is divided into five parts and then separated by three blank spaces.
Suppose i have a number 12345 then i want output like that 1 2 3 4 5



use mod 10 div 10 method.

java


int i=12345,rem;

while(i>0)
{
rem = i % 10;
i /=10;
System.out.print(rem);
System.out.print(" ");
}


I hope this will help you. smile.gif


This will print the digits in reverse order

CODE

int i = 12345;
String str = "" + i;                          // convert to String
char[] digit = str.toCharArray();      // String into array of char
for(int i = 0; i < digit.length; i++)
    System.out.print(digit[i] + " ");
System.out.println();


This post has been edited by pbl: 28 Jun, 2008 - 06:16 AM
User is online!Profile CardPM

Go to the top of the page

Programmist
post 3 Jul, 2008 - 01:37 PM
Post #5


Four-letter word

Group Icon
Joined: 2 Jan, 2006
Posts: 1,175



Thanked 6 times

Dream Kudos: 100

Expert In: Java

My Contributions


A little more concisely:
java
for(char c : String.valueOf(12345).toCharArray()) {
System.out.print(c + " ");
}
User is offlineProfile CardPM

Go to the top of the page

abgorn
post 4 Jul, 2008 - 10:47 AM
Post #6


D.I.C Addict

****
Joined: 5 Jun, 2008
Posts: 573



Thanked 2 times
My Contributions


Or you could make an array from 1-5, so when you need to specify them all just call the array and when you need the individual numbers call them seperately.

... or maybe, I'm not completely sure.

This post has been edited by abgorn: 4 Jul, 2008 - 10:49 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/15/08 04:30PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month