1

I have an array of 12 numbers

int ary2[] = {3,5,9,11,15,18,22,23,30,31,35,39};

I want to print the numbers out with 2 places for the number and a space between the numbers.

Example print out would be :

 3  5  9 11 15 18 22 23 30 31 35 39

This is how far I got.

for(int i = 0; i < ary2.length; i++)
{
    System.out.printf("%-3s", ary2[i]);
}

I'm new at this. Although I won't be directly submitting this as homework, it is affiliated with a homework project of mine; therefore, i'll be using that homework tag anyways.

EDIT: Answer Found.

6
  • 1
    I think the format of the desired output get lost when you posted the question without properly using code tags. Can you please edit the question again with the actual desired output? Commented Dec 2, 2009 at 21:58
  • Can you explain the problem with your existing code? The only things I can see is that it uses left alignment. I would prefer right-alignment. but it seems the left-alignment was intentional. The other thing is that it leaves unneeded trailing space at the line end. Other than that, it seems to work as expected. Commented Dec 2, 2009 at 22:03
  • I want each number to have a single space in between each other. With at least 2 spots designated for each integer in the array. Commented Dec 2, 2009 at 22:11
  • This is really confusing. I already answered that. Commented Dec 2, 2009 at 22:15
  • Phil, as already requested by BalusC: please edit your post and clearly indicate how the output should look like. As it is now: 3 5 9 11 15 18 22 23 30 31 35 39, clearly does not comply with what you're trying to tell us. Commented Dec 2, 2009 at 22:18

2 Answers 2

4

I am not quite sure if I understand the question. Your code example look like to already do that. Or do you want to treat them as digits (align right)? If so, then you need %d instead (digit) of %s (string). See if the following suits your needs:

System.out.printf("%2d ", ary2[i]);

This would print:

 3  5  9 11 15 18 22 23 30 31 35 39 

instead of what your original code did:

3  5  9  11 15 18 22 23 30 31 35 39 

you only have to live with the trailing space ;)

You can find more formatting rules in the java.util.Formatter API.

Sign up to request clarification or add additional context in comments.

2 Comments

But as you can see, there is 2 spaces in between the first 2 letters 3 and 5.
You literally said, you wanted 2 places for the number? What do you mean with that then?
0

Not sure what the problem is, reading the other answers and comments, I suspect that you want to display the numbers 0-padded:

    System.out.printf(" %02d", ary2[i]);

not 0-padded, 2 "places", like other already wrote:

    System.out.printf(" %2d", ...

I put the spaces at the start... but just my preferences

you can use some other character as separator insted of the space to see what's going on.
Something like System.out.printf("^%2d", ary2[i]);

If you want to avoid the space at the start or end of the line, you must split the output

    System.out.printf("%2d", ary2[0]);  // no space here
    for(int i = 1; i < ary2.length; i++)
    {
        System.out.printf(" %2d", ary2[i]);
    }

or do something like that (not my preferred)

    for(int i = 0; i < ary2.length; i++)
    {
        System.out.printf("%s%2d", (i == 0 ? "" : " "), ary2[i]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.