java - retrieve results from a table evaluating if the variable is within the interval of a row -


it's first time asking here , couldn't find me issue in related questions tables , rows intervals.

i'm working on simple aplication electrical purposes in java 7 using netbeans. thing there 3 tables this

isc/il----------tdd

10-20 -------5.0

21-50 -------8.0

51-100 -----12.0

101-1000 --15.0

1000+ -------20

each of tables has different values , 2 of them have same amount of rows. need evaluate x , check row belongs to tdd value.

all values double or float beacuse it's related physics , can't use int values.

the way can imagine if...else clause there many intervals , there 3 tables think impractical way because generate many lines of code single value.

if guys know other way done appreciate if share it. can make use of example can give me or library should through. if there's no other way i'll cup of coffee , start typing clauses lol.

also i'm not using database table, i'd use if necessary.

thanks in advance.

i did if...else clause because don't have time , code doesn't messy or bulky. here's code:

//gets value text field assigned variable val.vn , passed local variable vn;     double vn = val.vn = double.parsedouble(jtextfield9.gettext());     //using if clauses evaluate vn within 1 of 3 tables             if(vn <= 69){//table 1                     //a tdd assigned variable val.tdd according interval belongs                     if(val.iscil<=20 && val.iscil > 0){                             val.tdd = 5.0;                                   }else if(val.iscil > 20 && val.iscil <= 50){                             val.tdd = 8.0;                     }else if(val.iscil > 50 && val.iscil <= 100){                             val.tdd = 12.0;                     }else if(val.iscil > 100 && val.iscil <= 1000){                             val.tdd = 15.0;                     }else if(val.iscil > 1000){                             val.tdd = 20.0;                     }else{                         //shows error message should never seen knows, java surprises                             joptionpane.showmessagedialog(rootpane, "error al clasificar isc/il.", "extraño error o.o", joptionpane.error_message);                     }              }else if(vn>69 && vn<= 138){//table 2                     //a tdd assigned variable val.tdd according interval belongs                     if(val.iscil<=20 && val.iscil > 0){                             val.tdd = 2.5;                     }else if(val.iscil > 20 && val.iscil <= 50){                             val.tdd = 4.0;                     }else if(val.iscil > 50 && val.iscil <= 100){                             val.tdd = 6.0;                     }else if(val.iscil > 100 && val.iscil <= 1000){                             val.tdd = 7.5;                     }else if(val.iscil > 1000){                             val.tdd = 10.0;                     }else{                         //shows error message should never seen knows, java surprises                             joptionpane.showmessagedialog(rootpane, "error al clasificar isc/il.", "extraño error o.o", joptionpane.error_message);                     }              }else if(vn>138){// table 3                     //a tdd assigned variable val.tdd according interval belongs                     if(val.iscil<50 && val.iscil > 0){                             val.tdd = 2.5;                     }else if(val.iscil >= 50){                             val.tdd = 4.0;                     }else{                         //shows error message should never seen knows, java surprises                             joptionpane.showmessagedialog(rootpane, "error al clasificar isc/il.", "extraño error o.o", joptionpane.error_message);                     }             } 

this code executed button in interface. error message in spanish because it's local language here.

i still accept other way think achieved.

you shoudn't compare both lower , upper ranges:

//gets value text field assigned variable val.vn , passed local variable vn; double vn = val.vn = double.parsedouble(jtextfield9.gettext()); //using if clauses evaluate vn within 1 of 3 tables       if(vn <= 69){//table 1           //a tdd assigned variable val.tdd according interval belongs           if(val.iscil > 0) {               if (val.iscil<=20){                   val.tdd = 5.0;                             }else if(val.iscil <= 50){                   val.tdd = 8.0;               }else if(val.iscil <= 100){                   val.tdd = 12.0;               }else if(val.iscil <= 1000){                   val.tdd = 15.0;               }else                   val.tdd = 20.0;            } else{              //shows error message should never seen knows, java surprises              joptionpane.showmessagedialog(rootpane, "error al clasificar isc/il.", "extraño error o.o", joptionpane.error_message);            } 

and on.

and think it's readable solution. ternary operator (? :):

  if (val.iscil > 0) {       val.tdd = (val.iscil<=20) ? 5.0 :           (val.iscil <= 50) ? 8.0 :           (val.iscil <= 100) ? 12.0 :           (val.iscil <= 1000) ? 15.0 :           20.0   } 

but still lacks of beauty taste...


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -