Start New Discussion within our Software Development Community

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

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

String getSpacedString(String inp, int numSpaces)
{
  StringBuffer buf = new StringBuffer(inp.charAt(0));
  for (int i=1;i<inp.length();i++)
  {
    // do your magic here, that I'll leave to you. It's quite simple really ;)
  }
  return buf.toString();
}
This article has been dead for over six months. Start a new discussion instead.