Basic imperial conversion problem

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)

Asked by: Miller588 | Posted: 20-01-2022

Answer 1

Your types are wrong. You're parsing them as ints when they really should be doubles.


double imperial1 = Double.parseDouble(strValue3) + 
    (Double.parseDouble(strValue4) / 12.0);

Answered by: Julia953 | Posted: 21-02-2022

Answer 2

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

Answered by: Lydia533 | Posted: 21-02-2022

Answer 3

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. 

Answered by: John185 | Posted: 21-02-2022

Similar questions

java - Android: Conversion to Dalvik format failed: Unable to execute dex: null

I'm trying to use the SmugFig SmugMug API on Android. It was designed for J2SE I would imagine, so I'm not sure it will even work on Android, but I figured it was worth trying as opposed to trying to create my own API. When I load the project though, I get the following error: Conversion to Dalvik format failed: Unabl...

java - Conversion to Dalvik format failed error for Android Grid View

I'm on the android bandwagon and started going through google's "view" tutorials. Here is what I'm using: Eclipse Galileo Android SDK 2.1 Java SDK 6.Something I think. Everything was hunky-dory until I hit the grid view tutorial. I got errors all over the place when I started editing the "" File. I thought I'd fix it by following through with the next part of the tutorial, creating the ImageAdapte...

Android: Getting Error: Conversion to Dalvik format failed

I am building an app on android and running into an error and while searching on net, came across your posting on this and changed the eclipse.ini to increase Xms and Xmx params but still this error does not go away. I am using Eclipse IDE for Java with Android SDK 2.1 on Mac OS. Please help or please point me to someone who might know. Btw, this error only happens when i add external jar files (which i ne...

conversion of the input source to meaniful data in android

i want to ask about that can we convert the input source type data in android. my prob is here that i get the data from the url not in tags form. i fetch output in the inputsource type. after that i want to parse the data in meaniful form is this possible or not? if this possible then how can i implement? plz help

speech to text conversion in android

i want to develop apps based on speech to text conversion.can you please share your suggestions and how to develop this app

android - Java simple Timestamp to Date conversion

I've been trying to find the answer to this for a while today and there's just so much contradictory information.... What I'd like to do is get a current unix timestamp in android, and then convert it to a format that allows me to getHours() and getMinutes(). I'm currently doing this: int time = (int) (System.currentTimeMillis()); Timestamp ts = new Timestamp(time); mHour = ts.getHours(); mM...

iphone - develop speech to text conversion api for mobile os

develop one app speech to text conversion on mobile phones.i want to develop it for supporting all mobile(android,iphone,wince,windows 7,webos).for that i would like to develop one api. is there any way develop api for supporting all above os.Please Let me know and suggest me.any other way Thanks in advance aSwan

java - Dip to pixel conversion results in the exact same value

I am trying to convert dips to pixels using the following function. I’ve tried low, medium, and high density emulators and regardless of the emulator I use, the pixel value remains the same value as the dip value I pass into the function. private int ConvertDips(float dips) { int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dips, getResources().getDisplayMetrics()); ret...

android - Relative layout to Bitmap image conversion

i set background image to relativelayout and add some text in that layer. now i want to save the image into give me idea about this?.

conversion of jpeg to svg in android

I want to know how to convert jpeg files to svg files in android? Thank you in advance. Or can I convert pdf into svg format?

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android