expected<true>,but was<false> - android

I have a small application that converts temperature from Celsius to Fahrenheit and vice-versa. I have two RadioButtons to select the temperature i.e. Celsius or Fahrenheit and one Button and one EditText . I created a Junit to test the application.
Here is my test code:
float temparature=102;
Float expected=(float) 0;
solo.enterText(0,String.valueOf(temparature));
solo.clickOnButton("calculate");
if(solo.isRadioButtonChecked(0)){
expected=((temparature-32)*5/9);
}
else if(solo.isRadioButtonChecked(1)){
expected=((temparature*9)/5)+32;
}
String temp=expected+"";
assertEquals(true,solo.searchEditText(temp));
}
When I run the above test, test run was successful but failed saying: expected<true>but was <false>. I think there is some problem with value rounding. Please let me know what exactly is the problem.

You have String temp=expected+""; but expected is object(Float) type - Float expected.
So try expected.toString() or change Float expected to float expected.
And try to debug.

The value of expected is probably something like 102.0000001, which is not going to match the text in solo, which should be 102 (if I understand the code correctly).
You should follow the standard float comparison techniques instead of comparing Strings.
http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals%28java.lang.String,%20double,%20double,%20double%29
So you'd use something like:
assertEquals("The converted temperature didn't match.", temparature, expected, 0.001);
Overall, it isn't clear what your test case is trying to validate exactly. And the title of your post doesn't indicate anything about the question.

Related

Number Format Exception "Invalid Double"

I have to build a calculator and i am making some validations, the code is kinda long so i will only put the part that is breaking it.
It is a validation that makes the multiplication, it breaks when trying to multiply a negative number, every other operation is done correctly but the multiplication:
else if(resulttxt.getText().toString().contains("*")){
String resultarray=resulttxt.getText().toString();
String[] split=resultarray.split("\\*");
finalresult = Double.valueOf(split[0]) * Double.valueOf(split[1]);
if (finalresult<0){
negative=true;
}else{
negative=false;
}
resulttxt.setText(String.valueOf(finalresult));
resulttxt is received from a TextView that gets it's data from cliking on the numbers or the signs which are also TextViews (not buttons but they do have the On click listener)the operation is done when ciclking on the = sign.
This throws an error if for example i try to do -6*45:
java.lang.NumberFormatException: Invalid double: "6*45"
Like i said everything works with the addition,substraction and division it is only the multiplication that breaks.
I tried executing your code in the compiler :
String resulttxt = "-6*45";
boolean negative = false;
if(resulttxt.contains("*")){
String resultarray=resulttxt;
String[] split=resultarray.split("\\*");
double finalresult = Double.valueOf(split[0]) * Double.valueOf(split[1]);
if (finalresult<0){
negative=true;
}else{
negative=false;
}
System.out.println(finalresult);
}
Every thing worked fine for, please verify datatype used in your program.
addition, multiplication and division works fine. "-6+45, -6*45 and -6/45"(any other combination. I just used the same)
However for subtraction as the pattern is "-6-45" the above logic will fail and throw number format exception. This is because if you split "\-", the "-" is first character in string and there is nothing before it.
Thus your split will fail. So to avoid this you can always take last index of character to split using substring function.
OMG dudes... this is what was the problem:
I had this validation at the end of the other operations, so BEFORE even going to the multiplication part it entered the "-" validation when it checks
if(resulttxt.contains("-")){
because it is a negative value so it does have a "-"... so it entered as a substraction instead as a multiplication because of that...
To solve it i had to put the substraction validation at the bottom of all of them.
So thank you for the guys who suggested me to check the line where the error was thrown i wouldn't have known logcat actually tells you were the mistake is and to my surprise it was on the substraction validation which to me was a "WTF" moment and then i realized what i just told you.
Try this instead :
String s1 = resultarray.substring(0,resultarray.indexOf("*"));
String s2 = resultarray.substring(resultarray.indexOf("*")+1,resultarray.length());
double d1= Double.valueOf(s1);
double d2= Double.valueOf(s2);
Hope this helps

How to limit decimal digits - Android

I already searched online and all that I found is using DecimalFormat and I tried it, but when I code on Android Studio, appears a message saying that is necessary to use the API 24 to this kind of command. The API 24 has erros and all the sites I looked, advised to use the API 23.
So, I need a way to show double numbers only with two decimal digits on my AlertDialog.
referring to this SO post, you can use this code:
String formattedValue = String.format("%.2f", myDouble);
Let me know if it is what you were looking for
/** Round on two digits after "." */
public static float round(float f){
return Math.round(f * 100) / 100.0F;
}
P.S. Use float for short float numbers. Thats what its made for.
Edit:
"Ok, with 3 decimal digits its the better too,right? How can I limit those digits?"
Take a quick look how it works for two decimal places. Then its really simple to expand for three.

Calculator app approach

as getting into android i decided to replace the default calculator with mine. A simple calculator with the 4 operational signs. I've been giving to all buttons the right behaviour, storing every number in a 'num' ArrayList(String) and signs in a 'sign' ArrayList(String).
What i wanted to do, was to then combine numbers and signs into a string, parse it into a float and getting a result. I thought this was one of the easy/simple ways to deal with it, since when you set a float like this:
float f = 6*4-5/2+3
it gives you the right result. but it clearly does not when starting from a String, like this:
String s = "6*4-5/2+3"
Float f = Float.valueOf(s)
Is there a way to getting a result from my 2 ArrayList(String)? In the negative case, what would be a doable approach (in the sense im not an experienced programmer)I?
I Think this approach is incorrect.
I would do the following:
You would have a Textview or Edittext as the calculator "screen" on top.
then you would have all your number and operation signs buttons.
Now, every number you press, it will append to the last one on the screen, using .append()
once you tap on an operator sign - two things will happen:
1) the number in the textView will be stored as a Float (using Float.valueOf(yourTextView); in a varibale, say firstNum.
2) you will save the operator you clicked in a second variable, say String calcOper.
Now, you enter your second number, and then you would press the Equals sign.
What will happen then is simply use a Switch of If expression.
If calcOper is "-" - then do firstNum- Current number shown on screen.
If calcOper is "+" - then do firstNum+ Current number shown on screen.
At last don't forget to set the text on the TextView the result.
Good luck!

Android display float using settext method

iam allmoust done with my app but iam stuck on this thing , i have two int's
called "positive" and "negative" and when i procces source below it shows 0.0
total = positive + negative;
float rate = positive/total;
rate*=100;
TextView analitycs = (TextView)v.findViewById(R.id.app_scores_analitycs);
analitycs.setText(String.valueOf(rate));
What Victor said is true.
Also you might want to use something different than String.valueOf(rate) to set the text of your text view, because this method can give you an ugly representation of the number.
You should probably use String.format("%.2f", rate) ad tweak that to your needs.
Are positive and total floats/doubles?
If not, then an int/int will give you an int.
The solution would be to cast either positive or negative as a float.
try the following:
float rate = ((float)(positive))/total;

how to avoid exponents in android eclipse calculation results in a text string?

in android eclipse sometimes a calculation result for both double and float when displayed as a string uses a decimal point (desired) but sometimes using an exponent (bad - confusing to user). anyway to avoid the exponent?
See String.format documentation.
Just set the desired format for your numbers. You probably want String.format("%f",number).

Categories

Resources