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
Related
I am trying to make a BMI application. When I run the application the BMI values are displayed in numeral form that I don't understand. I have tried both Float and Double type but results are same.
For example:
Height (m): 2
Weight (Kg): 100
BMI is displayed as : 2.0E-4 instead of 25
The part of the code that effects this is:
String editText1= height_field.getText().toString();
String editText2= weight_field.getText().toString();
try { // Parse string to int
double height = Double.parseDouble(editText1);
double weight = Double.parseDouble(editText2);
double bmi_result = (weight/(height*height));
String bmi_text = Double.toString(bmi_result);
display.setText(bmi_text);
System.out.println("OnClick: computeButton is clicked");
}
catch(NumberFormatException nfe) {
alert.show(); // Show error alert
To answer your original question, you should be using java.text.DecimalFormat, something like:
DecimalFormat formatter = new DecimalFormat("##.##");
display.setText(formatter.format(bmi_result));
Will force the result to be in the format of two digits followed by two decimal points, the table in the link above shows how to generate that.
However, since 2.0E-4 is 0.0002, I think Jon Skeet's comment is correct: You're doing your math operation wrong, since the value you're printing is a very small fraction of 25 :)
I'd recommend using Log.v() to print out your math operation before you actually do it, so you can see what the values of weight and height actually are, I highly doubt they're correctly set at what you described in the question.
I can't seem to get my head around this. If have tried the following approaches:
DecimalFormat df = new DecimalFormat("00000.00");
DecimalFormat df = new DecimalFormat("######.##");
But the following line always generates and IllegalArgumentException.
double price = Double.valueOf(df.format(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString()));
// Sample input passed is value of 200 or some other whole number.
item.setPrice(price);
It doesn't make sense as I only copied the obvious solutions in this forum. Most of you got the format() to work.
Originally, I didn't have these lines of code. I just call my setPrice() method after getting the item price. This works. However, Double.valueOf() has a nasty habit of using only one decimal position.
e.g. passed 200. I get 200.0 inside my item object. I figured by using DecimalFormat I could've prevented this but it appears this caused me MORE headaches instead.
When you say you pass 200 and you get 200.0, you mean you get it in a double value? If so, that doesn't matter - it's a number and 200 = 200.0 for double values.
format(...) turns a double value to a String value. You have it the other way round. That's why you get the Exception.
If the price variable is actually a double you should do
double price = Double.valueOf(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString())
But I think you want that the price is a String, then you should convert the text from the EditText to a double and that double back to a String with something like new DecimalFormat("0.00")
I am now working on a calculator, and everything works fine except for decimal places.
The calculator contains 2 displays actually, one is called fakedisplay for actual operations, and one is called Display, for presenting the desired format, ie adding commas.
When pressing 12345.678, Display will follow fakedisplay and present as 12,345.678, but if i press 12345.009, the fakedisplay will work normally as 12345.009, but the Display stuck as 12,345 until 9 is pressed, and at that time it will show 12,345.009 normally.
However, it is strange that when the user presses 0, there is no response, and until pressing 9, 009 will then immediately append.
I know this arise from the parsing code, but based on this, how could I amend the following code? I really cannot think of any solution... Many thanks for all your advice!
one.setOnClickListener(new View.OnClickListener() {
if (str.length()<15) {
Fakedisplay.append("1");
}
DecimalFormat myFormatter1 = new DecimalFormat("###,###,###,###.#################");
String str1=Fakedisplay.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
stripped1 = myFormatter1.format(Double.valueOf(stripped1));
if (stripped1.endsWith(".0"))
stripped1 = stripped1.substring(0, stripped1.length() - 2);
Display.setText(stripped1);
}
Probably the easiest solution is to not strip off the .0 in the code for every keystroke..
Instead, only strip off trailing zeros (assuming there's a decimal point in there of course) when the user calls for a result. Entering keys such as the digit keys 0 through 9, the decimal point ., or the sign-change key +/- (what I'll call the entry keys) are not generating a result so should not strip trailing zeros.
However, non-entry keys, such as when you press + or - or = on your calculator can freely modify the number.
That will give you a display of the digits being entered as the user enters them but will still strip off trailing zeros when necessary.
You can do that with a modification to your statement (and, as mentioned, only doing this when the user presses a non-entry key):
stripped1 = stripped1.replaceAll("(\\.[0-9]*[1-9])0+$","$1");
stripped1 = stripped1.replaceAll("\\.0$","");
The first statement removes all trailing zeros at the end of a decimal number (other than on if it's really an integer). The second takes care of that case.
No doubt I could make a single substitution if I gave it some more thought but that should be enough to get it functional.
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.
I FINALLY have the map and points(arrays) working for my app. Quick question: I have a fatal exception with substring(), a "stringIndexOutOfBoundException"
In general, what is that referring to?
An I going past the end of a string using substring()?
Thanks,
testing.substring(1,2);
(I want to parse each character to find specific characters)
I wouldn't use substring() for grabbing 1-length strings (which is just a single character), but rather charAt(int) for specific positions. If you need to go over all characters in the string, you're probably better off with by converting the whole thing to a char[] first (using toCharArray()) and iterate over that.
Yes, you're going past the end of your strings bounds generally.
The Java API even tells you so...
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
You should get used to using the API. It tells you what exceptions a method throws and why.
Try printing the Strings length and value before attempting substring. That'll help you see the problem.
For example...
String testing = "Hello StackOverflow";
System.out.println("Length of testing = " + testing.length);
System.out.println("Value of testing = " + testing);
testing.substring(1,2);
Like stated in the official doc here:
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Throws: IndexOutOfBoundsException - if beginIndex is negative or
larger than the length of this String object.