简体   繁体   中英

Calculator float conversion

Well, im debugging my first android app and im having a problem with my program... My calculator only sums, subtracts, multiplies and divides... And the last operation is my problem... Im using TextEdit to insert any value on my phone but when I divide 5 by 3, for example, the answer is a float and the app will crash. It will only work if I use 5.0 and 3.0,

Is there anyway to convert ints to floats and ALWAYS answer a float?

So far this is what i've got for every operation...

try {
                firstNum = new BigDecimal(firstInput.getText().toString());
                firstFloat = firstNum.floatValue();
            } catch (NumberFormatException e) {
                firstNum = null;
            }

            try {
                secondNum = new BigDecimal(secondInput.getText().toString());
                secondFloat = secondNum.floatValue();
            } catch (NumberFormatException e) {
                secondNum = null;
            }

The "firstFloat = firstNum.floatValue();" and the "secondFloat = firstNum.floatValue();" i've seen on the forum a way of converting... Everything good until...

if (firstNum == null || secondNum == null) {
                Toast.makeText(getApplicationContext(), "Insira Valores!",
                        Toast.LENGTH_SHORT).show();
            } else {
                result.setText(firstNum.add(secondNum).toString());
            }
        }
    });

After entering the "else", this way it wont sum my inputs as floats unless I use the .0... Is there anyway to do all the operations using "firstFloat" and "secondFloat"?

Division:

div.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                firstNum = new BigDecimal(firstInput.getText().toString());
            } catch (NumberFormatException e) {
                firstNum = null;
            }

            try {
                secondNum = new BigDecimal(secondInput.getText().toString());
            } catch (NumberFormatException e) {
                secondNum = null;
            }

            if (firstNum == null || secondNum == null) {
                Toast.makeText(getApplicationContext(), "Insira Valores!",
                        Toast.LENGTH_SHORT).show();
            } else {
                BigDecimal div = firstNum.divide(secondNum).setScale(2,
                        BigDecimal.ROUND_HALF_EVEN);
                result.setText(div.toString());

            }
        }
    });

First: whenever your application "crashes" (exits due to an exception) and you want to ask for help in Stack, do the following before posting:

First, run it in Debug mode and look at the stack trace from the Debug window. If you don't see it, in Eclipse go to Window \\ Show View \\ Debug. In many cases, it will include something like:

Thread [<1> main] (Suspended (exception RuntimeException))
....
YourClass.yourMethod() line: 27
...

In this case you can see there's a RuntimeException in line 27 - that will help you pin down the problem. If you still need help and you need to post a question, include the stack trace, and point out which line is line 27.

If the stack trace doesn't show a failure in your code (in other words, if everything in the stack trace is Android classes and none of them are your classes), use breakpoints to figure out where the program is failing. Look at the Eclipse help for more info on setting breakpoints. You can also use them to figure out what's wrong with your program logic, because when you're in Debug mode and you hit a break point, you can hold your mouse over a variable and see what value it has.

On to your question. This works fine for me:

BigDecimal firstNum = null, secondNum = null;
float firstFloat = -1, secondFloat = -1;

try {
    firstNum = new BigDecimal("4");
    firstFloat = firstNum.floatValue();
    secondNum = new BigDecimal("3");
    secondFloat = secondNum.floatValue();
} catch (NumberFormatException e) {
    e.printStackTrace();
}
float a = firstFloat / secondFloat;
tempview.setText(a + "");

You didn't post the part where you do division, so I'm not sure why yours isn't working. However, instead of just posting the relevant part, I suggest that you should keep plugging at it yourself. Set some breakpoints at different points in your program and Debug it, and step through the code, checking the values of different variables as you go to see where the problem arises. Learning the process of debugging will be much more useful to you than the answer to this one question! Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM