Results 1 to 9 of 9
- 05-20-2012, 02:29 PM #1
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Assigning ranking numbers to String.format
Hey there. Ok i will try to explain.
My code is like this:
It gets first_name, last_name, tot_saldo from a database and the inputs 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());
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 ------------------------------------------------------------------------------------------------------------- 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 im not quite sure how to do this. I hoped you guys could give me a little assistance :)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
Regards
DawesomeLast edited by Fubarable; 05-20-2012 at 02:31 PM. Reason: code tags added
-
Re: Assigning ranking numbers to String.format
I've added [code] [/code] tags to the post above to try to help make it readable.
-
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?
- 05-20-2012, 02:52 PM #4
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Assigning ranking numbers to String.format
I have a SQLQuery which produces my desired order from the database.
The only problem (with this function ;) ) is ranking them #1 - #5 so that it's more obvious that it's a scoreboard :)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;
-
Re: Assigning ranking numbers to String.format
- 05-20-2012, 03:12 PM #6
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Assigning ranking numbers to String.format
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.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.
-
Re: Assigning ranking numbers to String.format
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.
- 05-20-2012, 03:42 PM #8
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Assigning ranking numbers to String.format
Thx for the help.
I just made a simple counter like this.
and incrementet it in myJava Code:int counter = 1;
withJava Code:text.format
Made my function an important part better ;)Java Code:counter ++
-
Re: Assigning ranking numbers to String.format
Good deal. Glad you've got it solved.
Similar Threads
-
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM -
JTable format numbers in column
By mine0926 in forum NetBeansReplies: 7Last Post: 06-07-2010, 05:10 AM -
Format Numbers
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 2Last Post: 03-07-2010, 07:57 PM -
Assigning a string value to a char
By coffeebean in forum New To JavaReplies: 4Last Post: 06-15-2008, 06:30 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks