4

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)

3 Answers 3

8

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);
3
  • And you might also want to add a try-catch block around the line, since parsing could throw an NumberFormatException. Jan 24, 2010 at 18:55
  • If you still want to parse the numbers as integers (I'm not sure why you would want to), you can still use Integer.parseInt, but you would have to cast the result to a double (explicitly, or by dividing by 12.0). I would do it Eli's way though since it's clearer and doesn't limit the input as much. Jan 24, 2010 at 19:07
  • Alex, can you explain what a try-catch block is? Also, how can I add a code that will check if the inputs are positive numbers and only run the calculation if all numbers are > 0?
    – Sean
    Jan 24, 2010 at 19:33
1

... 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" :)

1

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 of RuntimeException.

  • 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. 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.