Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Dawesome

Thread: Assigning ranking numbers to String.format

  1. #1
    Dawesome is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Assigning ranking numbers to String.format

    Hey there. Ok i will try to explain.

    My code is like this:

    Java Code:
    String text = "   Fist Name                  Last Name                                      Total Balance\n ";
    text += "--------------------------------------------------------------------------------------------------------------\n";
    					
    						for(ScoreboardDTO scoreboard : score)
    						 	{
    						 text += String.format(" %20s | %20s | %50d \n", scoreboard.getFirst_name(), scoreboard.getLast_name(), scoreboard.getTot_saldo());
    It gets first_name, last_name, tot_saldo from a database and the inputs like this:

    Java Code:
     Fist Name                  Last Name                                      Total Balance
    -------------------------------------------------------------------------------------------------------------
    
      user-name                user-lastname                                   user-total balance
      user-name2              user-lastname2                                  user-total balance2
      user-name3              user-lastname 3                                   user-total balance3
      user-name4              user-lastname4                                 user-total balance4
      user-name5              user-lastname4                                   user-total balance5
    But the problem is that it's a scoreboard of the persons with highest total balance on the database. Therefore i want java to give me a ranking of the top 5 like this instead:


    Java Code:
    Fist Name                  Last Name                                      Total Balance
    -------------------------------------------------------------------------------------------------------------
    
    #1  user-name               user-lastname                                   user-total balance
    #2 user-name2              user-lastname2                                  user-total balance2
    #3 user-name3              user-lastname 3                                   user-total balance3
    #4 user-name4              user-lastname4                                 user-total balance4
    #5 user-name5              user-lastname4                                   user-total balance5
    But im not quite sure how to do this. I hoped you guys could give me a little assistance :)

    Regards
    Dawesome
    Last edited by Fubarable; 05-20-2012 at 02:31 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Assigning ranking numbers to String.format

    I've added [code] [/code] tags to the post above to try to help make it readable.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Assigning ranking numbers to String.format

    Does the for loop produce names in order desired? Or do you need to sort the output first?

  4. #4
    Dawesome is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Assigning ranking numbers to String.format

    I have a SQLQuery which produces my desired order from the database.
    Java Code:
                            select u.first_name, u.last_name, max(h.tot_saldo) as tot_saldo from history_table h 
                		join user_table u on h.user_alias = u.user_alias 
                		join inst_table i on h.inst_id = i.inst_id
                		group by u.first_name, u.last_name
                		desc limit 5;
    The only problem (with this function ;) ) is ranking them #1 - #5 so that it's more obvious that it's a scoreboard :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Assigning ranking numbers to String.format

    Quote Originally Posted by Dawesome View Post
    I have a SQLQuery which produces my desired order from the database.
    Java Code:
                            select u.first_name, u.last_name, max(h.tot_saldo) as tot_saldo from history_table h 
                		join user_table u on h.user_alias = u.user_alias 
                		join inst_table i on h.inst_id = i.inst_id
                		group by u.first_name, u.last_name
                		desc limit 5;
    The only problem (with this function ;) ) is ranking them #1 - #5 so that it's more obvious that it's a scoreboard :)
    Don't use a for-each loop, but rather a standard for loop. Use the loop index + 1 for your rank value and concatenate it to your String. Bingo.

  6. #6
    Dawesome is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Assigning ranking numbers to String.format

    Don't use a for-each loop, but rather a standard for loop. Use the loop index + 1 for your rank value and concatenate it to your String. Bingo.
    Im actually using a while-loop. Is but can you give me an example ? :) I understand it but i'am not quite sure how to code it.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Assigning ranking numbers to String.format

    Quote Originally Posted by Dawesome View Post
    Im actually using a while-loop. Is but can you give me an example ? :) I understand it but i'am not quite sure how to code it.
    I much prefer you to take my advice and try to code this yourself, and then showing us your attempt and any questions if you have a problem.

    If you're using a while loop, then create an int counter variable before the loop, and increment it within the loop.

    I'm sure you can do this, so please give it a try or ask another question if not clear.

  8. #8
    Dawesome is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Assigning ranking numbers to String.format

    Thx for the help.
    I just made a simple counter like this.

    Java Code:
    int counter = 1;
    and incrementet it in my
    Java Code:
    text.format
    with
    Java Code:
     counter ++
    Made my function an important part better ;)
    Fubarable likes this.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  2. JTable format numbers in column
    By mine0926 in forum NetBeans
    Replies: 7
    Last Post: 06-07-2010, 05:10 AM
  3. Format Numbers
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 07:57 PM
  4. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 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
  •