This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I organize table rows as parent child relationship with a for loop and if statement  RSS feed

 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table with a PK, ID, ProductGroup

"1","10204","Regular"
"2","10203","Regular"
"3","10332","Regular"
"4","10201","Bold"
"5","10211","Bold"
"6","10130","New"
"7","10290","New"
"8","10126","New"
"9","10116","New"


I can obtain each row in a for loop, but how do I implement the logic to structure the data as:

Regular
10204
10203
10332
Bold
10201
10211
New
10130
10290
10126
10116


I've tried the following with the loop:



But I end up only getting the last two labels, if I set the header variable before the if statement inside or outside the for loop I end up printing out the header for each row.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you initialize "header" to be before the loop starts? If you initialize to the value of the first header, then you'd definitely get the results you're seeing.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried to initialize the "header" with the first header, which didn't work and I've tried it with an empty String.
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it a bit curious that you declare the "header" variable there in line 5. Perhaps you're assigning an initial value to some other "header" variable in the start of the loop, before that declaration?
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I ended up doing is running a SQL statement that returned the distinct header values and looped through those values and then pass the header value to the inside loop which allowed me to output the example above.

I appreciate the help!

Thanks.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you have N productGroups you're doing N+1 queries while it could be done in 1 query. That isn't a great solution.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are we discussing how to get a query to return results grouped and ordered by headers in SQL, or how to order them in Java™?
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:So if you have N productGroups you're doing N+1 queries while it could be done in 1 query. That isn't a great solution.


Completely agree, but I had to keep moving forward and I couldn't figure this out.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are we discussing how to get a query to return results grouped and ordered by headers in SQL, or how to order them in Java™?


The query works fine, I believe. I just need to use the correct logic in Java to output what I've described above. This is on a mobile device, so I'd like to remove the extra query if at all possible.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original solution was in the good direction:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!