0

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>
Adam
  • 35
  • 3

4 Answers4

5

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • ..And instead of the other `if` statements, you could use a ( possibly nested )`switch` statement. – Jonas Czech May 01 '15 at 20:46
  • what about if I add volume and weight, can Math.Pow() handle it ? – Adam May 01 '15 at 21:04
  • Math.Pow is just a function that raises a number to a certain power. It's part of your math, so if the volume and weight also require powers of 10, then yes, Math.Pow should accomodate you. – Robert Harvey May 01 '15 at 21:05
  • Just like to point out, you missed a step. When posTo = 4, you would get the wrong answer. – user2880486 May 01 '15 at 21:08
0

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

Community
  • 1
  • 1
ajacian81
  • 7,419
  • 9
  • 51
  • 64
0

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?*/
}
Karan
  • 2,120
  • 15
  • 27
0

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

user2880486
  • 1,068
  • 7
  • 16