Adam
Adam

Reputation: 35

Solution for very long (if statement)

I am new for programming and android development, I'm trying to make an app for length unit conversions. I used two spinner(from unit and to unit), so I need to check what user have selected in the both spinner and then return the value. My problem is I have got very long if statement, so I wonder if there is better solution for this.

Sorry for my poor english, I hope you understand what I mean.

here is the function for length:

public double lengthConversion() {
    double res = 0.0;

    num = Double.parseDouble(editTextNum.getText().toString());

    // first spinner is mm, second spinner is mm
    if (posFrom == 0 && posTo == 0) {
        res = num;
    } else if (posFrom == 0 && posTo == 1) { //second spinner is cm
        res =  num / 10;
    } else if (posFrom == 0 && posTo == 2) { // dm
        res =  num / 100;
    } else if (posFrom == 0 && posTo == 3) { // m
        res =  num / 1000;
    } else if (posFrom == 0 && posTo == 4) { // km
        res =  num / 1000000;
    }

    if (posFrom == 1 && posTo == 0) {
        ....
    } else if (posFrom == 1 && posTo == 1) {
        ....
    } else if (posFrom == 1 && posTo == 2) {
        ....
    } else if (posFrom == 1 && posTo == 3) {
        ....
    } else if (posFrom == 1 && posTo == 4) {
        ....
    }

    .
    .
    .


    if (posFrom == 4 && posTo == 0) {
        ...
    } else if (posFrom == 4 && posTo == 1) {
        ...
    } else if (posFrom == 4 && posTo == 2) {
        ...
    } else if (posFrom == 4 && posTo == 3) {
        ...
    } else if (posFrom == 4 && posTo == 4) {
        ...
    }

    return res;
  }

and that is the string array I use:

<string-array name="array_length">
    <item>mm</item>
    <item>cm</item>
    <item>dm</item>
    <item>m</item>
    <item>km</item>
</string-array>

Upvotes: 0

Views: 99

Answers (4)

user2880486
user2880486

Reputation: 1108

Sounds like you are doing unit conversion. Now assuming pos 0 = mm, pos 1 = cm and so on.

int myFrom = posFrom;
int myTo = posTo;
if(myFrom == 4) then myFrom = 6;
if(myTo== 4) then myTo= 6;
num = res * math.pow(10, myFrom - myTo);

realize 1km = 1000000mm that's why when pos = 4, you need to change it to 6 since 10^6 = 1000000

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180898

In your first else if tree, posTo is really just a power of ten, so make the calculation instead of passing it through all those if else statements.

if (posFrom == 0)
    res = num / Math.Pow(10, posTo);

All of your other units:

<string-array name="array_length">
    <item>mm</item>
    <item>cm</item>
    <item>dm</item>
    <item>m</item>
    <item>km</item>
</string-array>

are just variations on powers of ten, so perform a further calculation adjusting your values based on the selected units (compared to a reference unit, probably meters), and you should need no more than 5 cases or if elses.

Upvotes: 5

Karan
Karan

Reputation: 2130

You can use the nested switch:

Switch(posFrom)
{
    case 0 :    
            switch(posTo)
                          {
                            case 0:  /*do*/ break;
                            case 1:  /*do*/ break;
                          }
                 break;

    case 1:      
           switch(postTo)
                           {  
                           ... 
                           }
                 break;
    ...

    default: /*how did i land here?*/
}

Upvotes: 0

ajacian81
ajacian81

Reputation: 7599

You can have an enum with the base unit (meter for instance).

Then you can have each other units (kms, mms, etc) as fields of that enum. I was going to draw up an example but there's an answer that does this well:

https://stackoverflow.com/a/17549248/447842

Upvotes: 0

Related Questions