How do i print int num1; a 5 digit number like 1 2 3 4 5

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 1
Reputation: jollygreen is an unknown quantity at this point 
Solved Threads: 0
jollygreen jollygreen is offline Offline
Newbie Poster

How do i print int num1; a 5 digit number like 1 2 3 4 5

 
0
  #1
Jan 22nd, 2005
How do i print int num1; a 5 digit number like 1 2 3 4 5

ive been working this daxm problem for like 1-2 hours and cant get it to print
i dont want an answer i want a hint on what to do i want to work it trial and error my self so i learn it. I still have to make sure the num1 does not < 99999 or > 9999 but i think i know how to do that.

import java.util.Scanner;


public class Figure230

{

public static void main( String args[] )
{

Scanner input = new Scanner( System.in );


// This is where my integer is only one lonley guy
int num1;


System.out.print( "Please enter a five digit number:" ) ;
num1 = input.nextInt();




System.out.printf( " %d", num1 ); // it needs to display the 5 digits
//num with 3 space between each num.

}

}

would a couple more %d do the trick when i add another %d i get a error on compile
thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How do i print int num1; a 5 digit number like 1 2 3 4 5

 
0
  #2
Jan 22nd, 2005
You'll have to write your own parser for that. I know of no language that will allow you to do this with just one command.

The easiest would be to leave the number as a String (maybe pulling a validation over it to make sure it can be parsed into an int if that's a requirement, printing it out digit by digit using charAt() and putting the required number of spaces in between.
Something like
  1. String getSpacedString(String inp, int numSpaces)
  2. {
  3. StringBuffer buf = new StringBuffer(inp.charAt(0));
  4. for (int i=1;i<inp.length();i++)
  5. {
  6. // do your magic here, that I'll leave to you. It's quite simple really ;)
  7. }
  8. return buf.toString();
  9. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4873 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC