Reputation: 31
i created and mobile app that convert units,
one of the function in main java code that receive the number input and the unit that need to convert from and the unit that need to convert to , after that do calculation and return value, BUT, the problem is the function always return value 0.0
This the code below for the function:
public double areaConvert(double originalNumber, String originalUnit, String newUnit)
{ //Begin convertArea
//Make two doubles, one that holds the original and one that will be redefined where needed
//Make two strings, capturing the units fed to the method
String originalU = originalUnit.toLowerCase();
String newU = newUnit.toLowerCase();
double num1 = originalNumber;
double num2 = 0.0;
switch(originalU) {
//Begin unit conversions
case "12":
switch (newU) {
case "12":
num2 = originalNumber;
break;
case "14":
num2 = (num1 * 12) / 14;
break;
case "18":
num2 = (num1 * 12) / 18;
break;
case "21":
num2 = (num1 * 12) / 21;
break;
case "22":
num2 = (num1 *12) / 22;
break;
case "24":
num2 = (num1 *12) /24;
break;
}
break;
case "14":
switch (newU) {
case "12":
num2 = (num1 * 14) /12;
break;
case "14":
num2 = num1;
break;
case "18":
num2 = (num1 * 14) / 18;
break;
case "21":
num2 = (num1 * 14) /21;
break;
case "22":
num2 = (num1 *14) / 22;
break;
case "24":
num2 = (num1 *14) / 24;
break;
}
break;
case "18":
switch (newU) {
case "12":
num2 = (num1 * 18) /12;
break;
case "14":
num2 = (num1 * 18) / 14;
break;
case "18":
num2 = num1 ;
break;
case "21":
num2 = (num1 * 18) / 21;
break;
case "22":
num2 = (num1 * 18) / 22;
break;
case "24":
num2 = (num1 * 18) / 24;
break;
}
break;
case "21":
switch (newU) {
case "12":
num2 = (num1 * 21) /12;
break;
case "14":
num2 = (num1 * 21) / 14;
break;
case "18":
num2 = (num1 * 21) / 18;
break;
case "21":
num2 = num1 ;
break;
case "22":
num2 = (num1 * 21) / 22;
break;
case "24":
num2 = (num1 * 21) / 24;
break;
}
break;
case "22":
switch (newU) {
case "12":
num2 = (num1 * 22) /12;
break;
case "14":
num2 = (num1 * 22) / 14;
break;
case "18":
num2 = (num1 * 22) / 18;
break;
case "21":
num2 = (num1 * 22) / 21;
break;
case "22":
num2 = num1 ;
break;
case "24":
num2 = (num1 * 22) / 24;
break;
}
break;
case "24":
switch (newU) {
case "12":
num2 = (num1 * 24) /12;
break;
case "14":
num2 = (num1 * 24) / 14;
break;
case "18":
num2 = (num1 * 24) / 18;
break;
case "21":
num2 = (num1 * 24) / 21;
break;
case "22":
num2 = (num1 * 24) / 22;
break;
case "24":
num2 = num1 ;
break;
}
break;
} //End conversion table
return num2;
//Return the resulting number from the conversion table above
} //End convertArea
} //End converter class
Upvotes: 1
Views: 334
Reputation: 3767
If you pass 3.00, "12", "12" to the function it returns 3.00, exactly what you would expect. It is working the way it is supposed to. I don't understand what you mean by returning 0.0? The main issue is how big is the function going to be to test every number? It is designed really bad. You are also not handling unexpected values passed to this method, that will break your code and crash the app. I don't see the reason of toLowerCase(), when you are passing numbers.
Upvotes: 0
Reputation: 276
Seems to me like what you need is
num2 = num1 * Double.parseDouble(originalU) / Double.parseDouble(newU);
Upvotes: 1
Reputation: 11
It likely returns zero because you're not executing any case clause that would set it to not-zero.
I suggest coding 'default:' cases to detect inappropriate input. Maybe just print out "Can't get here" with the value of whatever you're switching on.
Upvotes: 1