Question

It's my first time asking here and I couldn't find something that could help me with this issue in related questions about tables and rows with intervals.

I'm working on a simple aplication for electrical purposes in Java 7 using netbeans. The thing is that there are 3 tables that look like this

Isc/Il----------TDD

10-20 -------5.0

21-50 -------8.0

51-100 -----12.0

101-1000 --15.0

1000+ -------20

each of the tables has different values and 2 of them have the same amount of rows. I need to evaluate X and check which row it belongs to to get a TDD value.

All values are double or float beacuse it's related to physics and can't use int values.

The only way I can imagine to do this is with if...else clause but as there are too many intervals and there are also 3 tables i think it would be impractical to do it this way because it would generate so many lines of code for a single value.

if you guys know any other way it could be done I would appreciate if you share it. I can make use of any example you can give me or any library I should look through. if there's no other way I'll get a cup of coffee and start typing those clauses lol.

Also i'm not using any database for the table, but I'd use it if necessary.

Thanks in advance.

I already did it with if...else clause because I don't have much time and the code doesn't look so messy or bulky. Here's the code:

//Gets the value from the text field which is assigned to the variable val.vn and also passed to the local variable vn;
    double vn = val.vn = Double.parseDouble(jTextField9.getText());
    //Using if clauses to evaluate vn within one of the 3 tables
            if(vn <= 69){//table 1
                    //A tdd is assigned to the variable val.tdd according to the interval it belongs to
                    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 an error message that should never be seen but who knows, java always surprises us
                            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 is assigned to the variable val.tdd according to the interval it belongs to
                    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 an error message that should never be seen but who knows, java always surprises us
                            JOptionPane.showMessageDialog(rootPane, "Error al clasificar Isc/IL.", "Extraño Error O.o", JOptionPane.ERROR_MESSAGE);
                    }

            }else if(vn>138){// table 3
                    //A tdd is assigned to the variable val.tdd according to the interval it belongs to
                    if(val.iscil<50 && val.iscil > 0){
                            val.tdd = 2.5;
                    }else if(val.iscil >= 50){
                            val.tdd = 4.0;
                    }else{
                        //shows an error message that should never be seen but who knows, java always surprises us
                            JOptionPane.showMessageDialog(rootPane, "Error al clasificar Isc/IL.", "Extraño Error O.o", JOptionPane.ERROR_MESSAGE);
                    }
            }

This code is executed by a button in the interface. The error message is in spanish because it's the local language here.

I still accept any other way you think this could be achieved.

Was it helpful?

Solution

You shoudn't compare both lower and upper ranges:

//Gets the value from the text field which is assigned to the variable val.vn and also passed to the local variable vn;
double vn = val.vn = Double.parseDouble(jTextField9.getText());
//Using if clauses to evaluate vn within one of the 3 tables
      if(vn <= 69){//table 1
          //A tdd is assigned to the variable val.tdd according to the interval it belongs to
          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 an error message that should never be seen but who knows, java always surprises us
             JOptionPane.showMessageDialog(rootPane, "Error al clasificar Isc/IL.", "Extraño Error O.o", JOptionPane.ERROR_MESSAGE);
           }

and so on.

And I personally think it's almost the most readable solution. Certainly you could do this with 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 it still lacks of beauty to my taste...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top