gaurav tiwari gaurav tiwari - 2 months ago 16
Android Question

Height Conversion - cm to feet and inches (and vice versa)

I have an edit text in which the user can enter his height in centimeters as well as in feet+inches, eg. 5'11". I have a toggle button for the target unit, so I want that when the user selects centimeters, it should convert the entered text from feet+inches into centimeters, and vice versa.
Now when I am converting the height to centimeters it's adding "\"" at the end. I think it's because of the text watcher I have put which add "\"" at the end when count is reaching to 3.

public void onClick(View view) {
switch (view.getId())
{
case R.id.btnCm:
toggleHeightButton(R.id.btnCm,R.id.btnFeet,false);
convertToCentimeter(enter_height);
break;
case R.id.btnFeet:
toggleHeightButton(R.id.btnFeet,R.id.btnCm,true);
enter_height.addTextChangedListener(new CustomTextWatcher(enter_height));
break;
case R.id.btnKg:
toggleweightButton(R.id.btnKg,R.id.btnpound,false);
break;
case R.id.btnpound:
toggleweightButton(R.id.btnpound,R.id.btnKg,true);
break;
}
}

public class CustomTextWatcher implements TextWatcher {
private EditText mEditText;

public CustomTextWatcher(EditText enter_height) {
mEditText = enter_height;
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

public void afterTextChanged(Editable s) {
int count = s.length();
String str = s.toString();
if (count == 1) {
str = str + "'";
} else if (count == 2) {
return;
} else if (count == 3) {
str = str + "\"";
} else if ((count > 4) && (str.charAt(str.length() - 1) != '\"') ){
str = str.substring(0, str.length() - 2) + str.charAt(str.length() - 1) + "\"";
} else {
return;
}

mEditText.setText(str);
mEditText.setSelection(mEditText.getText().length());
}
}

Answer

This can be easily done with regular expressions, but I think you should try the more straightforward way first.

Basically, the format is like xx'xx". We can split the string using the ' deliminator. That way, the first item of the array is the number of feet.

Then, we have the second item of the split string left: xx". For this, we just need to substring it to remove the last character and then we can get the number of inches!

Try to write the code yourself!


If you're really stuck, here's the solution:

String str = s.toString();
String[] splitString = str.split("'");
String firstItem = splitString[0];
try {
    int feet = Integer.parseUnsignedInt(firstItem);
    String secondPart = splitString[1].substring(0, splitString[1].length() - 1);
    int inches = Integer.parseUnsignedInt(secondPart);
    // YAY! you got your feet and inches!
    System.out.println(feet);
    System.out.println(inches);
} catch (NumberFormatException e) {
    return;
}

And here's a solution using regex:

String str = s.toString();
Pattern pattern = Pattern.compile("(\\d+)'((\\d+)\")?");
Matcher matcher = pattern.matcher(str);
if (!matcher.matches()) {
    return;
}

int feet = Integer.parseUnsignedInt(matcher.group(1));
String inchesStr = matcher.group(3);
int inches = 0;
if (inchesStr != null) {
    inches = Integer.parseUnsignedInt(inchesStr);
}

// YAY! you got your feet and inches!
Comments