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

Dani AI

Generated

A few clarifications and practical options based on the thread: adding another %d without supplying a second argument causes the compile error seen in 's post — each format specifier must have a matching argument. Also note that nextInt() will drop leading zeros (so 01234 becomes 1234); if leading zeros must be preserved the input should be handled as a String, as suggested.

Arithmetic (int) approach — extracts digits with division/modulo and prints them with three spaces:

int n = Math.abs(num1);
if (n >= 10000 && n <= 99999) {
    int[] d = new int[5];
    int tmp = n;
    for (int i = 4; i >= 0; i--) {
        d[i] = tmp % 10;
        tmp /= 10;
    }
    System.out.println(d[0] + "   " + d[1] + "   " + d[2] + "   " + d[3] + "   " + d[4]);
}

String approach — keeps leading zeros and makes validation easy (use when "01234" must remain five characters):

String s = input.nextLine().trim();
if (s.matches("\\d{5}")) {
    StringBuilder out = new StringBuilder();
    for (int i = 0; i < 5; i++) {
        if (i > 0) out.append("   ");
        out.append(s.charAt(i));
    }
    System.out.println(out.toString());
}

Troubleshooting notes: validate the input range (10000..99999) when working with ints; use Math.abs() if negatives should be treated by magnitude; catch InputMismatchException or use hasNextInt() when reading ints; prefer the String route if preserving formatting or leading zeros is required. The arithmetic loop is a small, instructive exercise; the String method is easiest for input-validation and formatting.

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.