I am just wondering if I could get some advice on implementing an algorithm for creating a JFreeChart. Basically, I am extracting some data from a JTable which contains information about patients. There are age categories for all patients such as 20-26, 26-30, 31-35, 35-40, 50-55, 55-60 etc. There are about 30 of them and every patient belongs to their corresponding age category. There is a button on top of the JTable which opens a frame containing the age distribution graph. What I am thinking of doing:
I suppose this might be a relatively good way of doing this but I was wondering if somebody could possibly suggest a way of avoiding having to create a variable for every category and then having to do all those comparisons using about 30 if statements.
EDIT: I do not have the patient's exact age - only the category they belong to.
I've assumed you've got your own class AgeRange which stores a range of ages. Then what you can do is store the age ranges in a TreeMap<Integer,AgeRange>, where the key is the first number of the range and the value is the range itself.
When you need to find which age range contains a particular age, use
theMap.lowerEntry(age + 1)
to find it.
Check the TreeMap Javadoc at http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With