Coding a calculation tool for my android. On of the开发者_开发问答 inputs is distance in Feet and Inches.
I have two inputs (input3 and input4) for feet and inches, respectively. In my calculation I am trying to convert these two inputs to a decimal number to be used in the rest of the equation. Here's the part of my code that does this:
private void doCalculation() {
// Get entered input value
String strValue3 = input3.getText().toString();
String strValue4 = input4.getText().toString();
// Perform a hard-coded calculation
double imperial1 = (Integer.parseInt(strValue3) + (Integer.parseInt(strValue4) / 12));
// Update the UI with the result to test if calc worked
output2.setText("Test: "+ imperial1);
}
My test values are 4 feet, 6 inches. The 4 comes across fine, but the 6 inches defaults to 0 when it is divided by 12. So my result is 4.0 I tried cutting the calculation down to JUST the division operation, and the result was 0.0
What am I doing wrong? (fyi: this is my first time using Java)
Your types are wrong. You're parsing them as ints when they really should be doubles.
Try:
double imperial1 = Double.parseDouble(strValue3) +
(Double.parseDouble(strValue4) / 12.0);
... what Eli said.
I just wanted to ask why your variables are called strValue3 and strValue4. I'm guessing that it's generated, but you should get into the habit of naming things well. I might go with "feet" and "inches" :)
Eli's answer will work fine. I'm just posting an answer to your question (comment) on Eli's answer.
Can you explain what a try-catch block is?
First you have to understand what an exception is. An exception is an event, which occurs during the execution of your program, that disrupts the normal flow of the program's instructions.
There are 3 kinds of exceptions in Java:
Runtime exceptions: An exception is referred to as a runtime exception if its data type is
java.lang.RuntimeException
or a subclass of it.Checked exceptions: An exception is referred to as a checked exception if its data type is a child class of
java.lang.Exception
, but not a child class ofRuntimeException
.Errors: An exception is referred to as an error if its data type is a child class of
java.lang.Error
. An error is associated with problems that arise outside of your application and typically do not attempt to recover from errors.
For a more detailed description on exceptions, read this.
In order to catch and handle these exceptions, you use try-catch blocks. For example, you use Double.parseDouble
function. If the parameter in this function is not a valid number, for example if the user supply the string "NotANumber" you try to convert it to double, then a NumberFormatException
will be thrown by Double.parseDouble
. If you don't handle this error, your program will terminate unexpectedly.
So, you should write something like the following (including the positive numbers feature you want):
double imperial1 = 0.0;
try {
double firstNumber = Double.parseDouble(strValue3);
double secondNumber = Double.parseDouble(strValue4);
if(firstNumber < 0 || secondNumber < 0)
throw new NumberFormatException("numbers must be positive.");
imperial1 = firstNumber + secondNumber / 12.0;
} catch(NumberFormatException ex) {
// Handle the exception maybe by printing a message to the user that his inputs
// weren't valid numbers.
}
精彩评论