• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Answered: java help for simple converter

xDroidApps

Lurker
Oct 11, 2011
6
0
hi, i am making a simple converter to learn Droid app development but i am having a problem. Here is my code:

package xapp.xylia.converter;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DistanceScreen extends Activity implements View.OnClickListener{

Button kmTomi, miTokm;
TextView calDistance;
EditText enterDistance;
String sDistance;
Double dDistance, answerDistance;
DecimalFormat df = new DecimalFormat("#.##");

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.distancescreen);
asButton();
kmTomi.setOnClickListener(this);
miTokm.setOnClickListener(this);
}
private void asButton() {
// TODO Auto-generated method stub
kmTomi = (Button) findViewById(R.id.bkmTomi);
miTokm = (Button) findViewById(R.id.bmiTokm);
calDistance = (TextView) findViewById(R.id.displayDistance);
enterDistance = (EditText) findViewById(R.id.textDistance);
}
public void onClick(View v) {
// TODO Auto-generated method stub
sDistance = enterDistance.getText().toString();
dDistance = Double.parseDouble(sDistance);

switch(v.getId()){
case R.id.bkmTomi:
answerDistance = dDistance * 0.621371192;
calDistance.setText("≈" + df.format(answerDistance) + "mi");
enterDistance.setText("");
break;
case R.id.bmiTokm:
answerDistance = dDistance * 1.609344;
calDistance.setText("≈" + df.format(answerDistance) + "km");
enterDistance.setText("");
break;
}
}
}

the problem is if the user imputs nothing, the program force closes. i can fix that by setting the SetText value to "0", but then it makes it harder to enter a second value because the 0 is added to the end unless deleted. How can i solve this? I tried: if sDistance != "" then double parse else =0.0 but it didn't work.

Thanks
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones