# How i separate digits with 3 spaces

Page 1 of 1

## 9 Replies - 5843 Views - Last Post: 08 January 2010 - 04:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=148717&amp;s=424a926821e26a7cd0e246a6f0daf624&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Akilles

Reputation: 0
• Posts: 24
• Joined: 07-January 10

# How i separate digits with 3 spaces

Posted 07 January 2010 - 05:06 PM

package prog2;
import javax.swing.JOptionPane;
public class Main {
public static void main (String [] args){
String Number1; int Number;
int D1, D2,D3,D4,D5,result;
Number1 = JOptionPane.showInputDialog ("Give a 5 digit number");
Number = Integer.parseInt (Number1);

while (Number < 10000 || Number > 99999) {

Number1 = JOptionPane.showInputDialog ("Please Give a 5 digit number only");
Number = Integer.parseInt (Number1);
}
D1=Number/10000;
result=Number%10000;

D2=Number/1000;
result=Number%1000;

D3=Number/100;
result=Number%100;

D4=Number/10;
result=Number%10;

D5=Number/1;
result=Number%1;

JOptionPane.showMessageDialog(null,"The Digits of your number is" ex: 1   2   3   4   5 );
System.exit(0);

}
}

I want to put here its individual digits and prints the digits separated from one another by three spaces each...how do i do that???i cant separate them

This post has been edited by Akilles: 07 January 2010 - 05:08 PM

Is This A Good Question/Topic? 0

## Replies To: How i separate digits with 3 spaces

### #2 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:07 PM

Could you please post any problems you are having in the body of your post, rather than the title? Thanks

Also, what's wrong with using this:
D1 + "   "  + D2 + "   " + D3 + "   "  //...

This post has been edited by erik.price: 07 January 2010 - 05:10 PM

### #3 Akilles

Reputation: 0
• Posts: 24
• Joined: 07-January 10

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:11 PM

Sorry i'll keep it in mind....I am new here so i don't know how it works

### #4 Akilles

Reputation: 0
• Posts: 24
• Joined: 07-January 10

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:19 PM

Eric it doesn't work....i must be somewhere wrong....

JOptionPane.showMessageDialog(null,"The Digits of your number is:" + D1,"   ", + D2,"   ", + D3,"   ",+D4,"   ",+D5);

when i write this... the showMessageDialog goes red (it means somewhere i have wrong... :-/ ) Where is it???i just cant find it...

### #5 chili5

• D.I.C Lover

Reputation: 20
• Posts: 1,145
• Joined: 28-December 07

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:26 PM

JOptionPane.showMessageDialog(null,"The Digits of your number is:" + D1 + "   " + D2 + "   " + D3 + "   " +D4 + "   "+D5);

### #6 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:26 PM

D1,"   ", + D2,"   ", + D3,"   ",+D4,"   ",+D5

Commas are used (among other things) to separate arguments in a method call.

What you are looking for is the concatenation operator + which forms a new String out of two original ones.

Try the code I posted before and see how that works:
D1 + "   "  + D2 + "   " + D3 + "   " + D4 + "   " + D5

### #7 Akilles

Reputation: 0
• Posts: 24
• Joined: 07-January 10

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:32 PM

I put your code as you gave it to me but now it shows in the inputDialog a message that goes like this.... 1 12 123 1234 12345
I want to be like this.... 1 2 3 4 5

Is there something wrong with the code i have written above???

### #8 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: How i separate digits with 3 spaces

Posted 07 January 2010 - 05:38 PM

It has to do with how you found the digits.

You could try keeping String, Number a String instead of converting it to an int, and then setting D1, D2, D3, D4, D5 to Number.charAt(1/2/3/4/5)

### #9 g00se

• D.I.C Lover

Reputation: 3425
• Posts: 15,484
• Joined: 20-September 08

## Re: How i separate digits with 3 spaces

Posted 08 January 2010 - 02:12 AM

Try

String digits = ("" + Number).replaceAll("(\\d)", "\$1   ");
JOptionPane.showMessageDialog(null, "The Digits of your number are "  + digits);

or with more precision:

String digits = ("" + Number).replaceAll("(\\d)", "\$1   ").trim();

This post has been edited by g00se: 08 January 2010 - 02:15 AM

### #10 Akilles

Reputation: 0
• Posts: 24
• Joined: 07-January 10

## Re: How i separate digits with 3 spaces

Posted 08 January 2010 - 04:11 AM

Go0se thats it!!!!!Thank you all very much!!!!!!!!