Results 1 to 5 of 5

Thread: printf

  1. #1
    razzle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default printf

    Heya, I'm doing an assignment and need to print an array in:

    System.out.println(numbers[0] + " " + numbers[1] + " " + numbers[2] + " " + numbers[3] + " " + numbers[4]);
    System.out.println(numbers[5] + " " + numbers[6] + " " + numbers[7] + " " + numbers[8] + " " + numbers[9]);
    System.out.println(numbers[10] + " " + numbers[11] + " " + numbers[12] + " " + numbers[13] + " " + numbers[14]);
    System.out.println(numbers[15] + " " + numbers[16] + " " + numbers[17] + " " + numbers[18] + " " + numbers[19]);
    System.out.println(numbers[20] + " " + numbers[21] + " " + numbers[22] + " " + numbers[23] + " " + numbers[24]);
    System.out.println(numbers[25] + " " + numbers[26] + " " + numbers[27] + " " + numbers[28] + " " + numbers[29]);
    System.out.println(numbers[30] + " " + numbers[31] + " " + numbers[32] + " " + numbers[33] + " " + numbers[34]);
    System.out.println(numbers[35] + " " + numbers[36] + " " + numbers[37] + " " + numbers[38] + " " + numbers[39]);
    System.out.println(numbers[40] + " " + numbers[41] + " " + numbers[42] + " " + numbers[43] + " " + numbers[44]);
    System.out.println(numbers[45] + " " + numbers[46] + " " + numbers[47] + " " + numbers[48] + " " + numbers[49]);

    this sort of format, but each value needs to be three digits long with leading zeros, I've found out that by using the formula (i.e. 3 goes to 003, or 10 to 010)
    System.out.printf("%03d", numbers[0]);
    it will do that, but i cant seem to get more than one on each line, is this possible?

    Note: numbers is the name of the array.

    Thankies for reading

  2. #2
    funnyboy's Avatar
    funnyboy is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Wink

    You just loop through it:

    int count= 0;
    for(count==0; count<=numbers.lenght; count++){
    System.out.printf("%d, ", numbers[count]);
    }

  3. #3
    razzle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I would do that but its got to be in a 5*10 grid. can you do that in a for loop?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by razzle View Post
    I would do that but its got to be in a 5*10 grid. can you do that in a for loop?
    .........Yes

  5. #5
    funnyboy's Avatar
    funnyboy is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Lightbulb

    Just initialze a second count variable and everytime it is 5, move to the next line!

Similar Threads

  1. Printf problem!!!
    By pinar in forum New To Java
    Replies: 3
    Last Post: 11-06-2010, 10:44 PM
  2. Few errors with for loop/printf
    By ks1615 in forum New To Java
    Replies: 8
    Last Post: 03-12-2009, 10:47 PM
  3. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 07:59 AM
  4. printf error with int and double
    By o0n1 in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 09:58 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •