• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Detect Value Change in Loop

 
Ranch Hand
Posts: 2140
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May it has just been a long week. I am using while loop to iterate over a ResultSet(remote data) building an array. One of the columns can have several records with the same value I would like to add an element to my array when this column changes value.

Say the first set of values I would set a flag to "Odd". Then when the value changes set the flag to "Even". Upon changing again go back to "Odd". I want to use this flag to set alternating colors in the table the arry will be populating.

So the first set of records, 3 line items, would be item number '856898-401' gray. The second set, 5 line items, '856898-403' would be yellow. Then the next set would revert back to gray.

This is my loop code:

 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what your question is. At first, I thought the implied question was, "Why doesn't this work?" Looking at your code though, I don't see that you've made any attempt to implement the functionality you describe. Is your question, "Will someone do this for me?" That's not really what we do here. Try to do your own work, and if you run into problems, we can help guide you. You'll want to hold the previous value in a variable, and compare it with the current value.
 
Steve Dyke
Ranch Hand
Posts: 2140
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:I'm not sure what your question is. At first, I thought the implied question was, "Why doesn't this work?" Looking at your code though, I don't see that you've made any attempt to implement the functionality you describe. Is your question, "Will someone do this for me?" That's not really what we do here. Try to do your own work, and if you run into problems, we can help guide you. You'll want to hold the previous value in a variable, and compare it with the current value.



I did have some attemp code but I took it out for clearity. I am not asking someone to do it for me just some help in the right direction. I realize the current value will have to be stored and a comparison made. My issue is the value is not known until the loop starts. So I set my variable = "" outside the loop. Within the loop, If record value does not match(which it won't) I set my flag to "Odd", set the variable to record value. Where do I go from there?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First:
How does the value change?

Second:
What is the color id, how is it used, and are you sure you should be worrying about it in the data access code (instead of, for example, in the display code).

Finally:
It looks like you are using an ArrayList to hold Strings that have different meaning. Perhaps you should create a Data Object to hold those values in a more meaningful way.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:I am using while loop to iterate over a ResultSet(remote data) building an array. One of the columns can have several records with the same value I would like to add an element to my array when this column changes value.


So, am I right in thinking that you only want to display rows from your ResultSet where a specific column has a distinct value?

If so, you might want to think about a Map keyed by that column; though how you go about choosing the other values to display I really have no idea. Alternatively (and possibly better), write your SQL query so that it only returns distinct values for that column. That's what databases were designed for, after all.

Another tip: From the sound of it, you're mixing display issues (different coloured columns) with the process of extracting what you want to display. Keep these things completely separate. Indeed, I'd say that you should be able to test the data portion of your program without any kind of GUI whatsoever.

Winston
 
Steve Dyke
Ranch Hand
Posts: 2140
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Steve Dyke wrote:I am using while loop to iterate over a ResultSet(remote data) building an array. One of the columns can have several records with the same value I would like to add an element to my array when this column changes value.


So, am I right in thinking that you only want to display rows from your ResultSet where a specific column has a distinct value?

If so, you might want to think about a Map keyed by that column; though how you go about choosing the other values to display I really have no idea. Alternatively (and possibly better), write your SQL query so that it only returns distinct values for that column. That's what databases were designed for, after all.

Another tip: From the sound of it, you're mixing display issues (different coloured columns) with the process of extracting what you want to display. Keep these things completely separate. Indeed, I'd say that you should be able to test the data portion of your program without any kind of GUI whatsoever.

Winston



The user will be presented a list of rows reresenting records for data processing, Serial Number, Part Number, Description as such:

00123 855999-401 Seat
00124 855999-401 Seat
00125 855999-401 Seat
00126 855888-401 Seat
00127 855888-401 Seat
00128 888111-403 Seat
00129 888111-403 Seat

The first 3 rows have the same part number so they would be gray.
The next 2 rows have the same part number but are different from the previous part number so they would be yellow.
The next 2 rows have the same part number but are different from the previous part number so the would go back to gray.
And so on. Alternating the color as the part numbers, by group changes.
 
Sheriff
Posts: 16938
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To reiterate what Winston already already said, keep display concerns separate from data access concerns. If you are using JSPs to display the table of data then you can combine conditional logic and CSS classes to control the color of table rows.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:To reiterate what Winston already already said, keep display concerns separate from data access concerns. If you are using JSPs to display the table of data then you can combine conditional logic and CSS classes to control the color of table rows.


Not being a GUI expert, I'd like your opinion; because my first thought when I saw that data was that I would actually process it to create an undisplayed "group break" field (perhaps a number), simply to minimize the processing requirements for the GUI - and also to decouple what it represents from how it's processed for display. Is this a reasonable approach or merely a manifestation of my dislike of all things GUI and Webby?

Asked partly in jest, but I'd really would be interested to know how a more "screen oriented" designer might look at the problem, because I'm quite sure that there are many solutions.

Winston
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you are working on a rich client you might want to have in mind to not run long tasks inside the ui thread in addition to seperation of concerns.
 
Junilu Lacar
Sheriff
Posts: 16938
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:I'd like your opinion; because my first thought when I saw that data was that I would actually process it to create an undisplayed "group break" field (perhaps a number), simply to minimize the processing requirements for the GUI - and also to decouple what it represents from how it's processed for display. Is this a reasonable approach or merely a manifestation of my dislike of all things GUI and Webby?



My current approach: when it makes sense to do so, I create a separate "view model", which I suppose is in line with what you describe in creating an undisplayed "group break" field. A naive/beginner (misguided) implementation might make this "group break" thing tightly coupled with a display-related value, such as a CSS class, where your view model has "knowledge" of the different CSS class names that you will use to display alternate row colors. This is not a good implementation because it couples your view model to the presentation too tightly. My initial design would probably be to just sort the data by the "group id". Then in the JSP, if that was the presentation technology being used, I would set up a loop to iterate through the data set, remembering the group id at the start and changing a JSTL variable each time the group id changed. The JSTL variable is what would control the CSS class name that would get rendered for each row.

For me, it just boils down to limiting the scope of things as much as possible: the more limited the scope the better because that tends to limit the number of things getting coupled to it. Conversely. the wider the scope, the more chances there are of other things getting coupled to it.
 
I knew that guy would be trouble! Thanks tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic