Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with data from table

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:

  1. Create an integer variable for every category
  2. Loop through the age details for all patients in the JTable
  3. Compare variables with the JTable data and increment by 1 if there is a match (lots of if statements to go in the loop)
  4. Store the categories names and the amount of people registered under every category in a HashMap
  5. Pass the map to the ChartFrame

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.

like image 270
user3281466 Avatar asked Apr 17 '26 02:04

user3281466


1 Answers

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

like image 87
Dawood ibn Kareem Avatar answered Apr 18 '26 16:04

Dawood ibn Kareem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!