How i separate digits with 3 spaces

I want to separate and show the digits of a 5 digit number...

Page 1 of 1

9 Replies - 4741 Views - Last Post: 08 January 2010 - 04:11 AM Rate Topic: -----

#1 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#5 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • 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);


Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!!!!!! :D :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1