Android program problem

This single activity program, when run on Eclipse AVD, is accepting values, but not printing the value when button is clicked. I want to ask the error, if any and the reason why the program is not giving the desired results.

package com.msi.manning.calc_ur_bmi;

import android.os.;
import android.app.Activity;
import android.view.
;
import android.widget.;
import android.net.
;
import android.content.*;
import java.text.NumberFormat;
import android.util.Log;

public class Calc_ur_BMI extends Activity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
final EditText weightfield=(EditText) findViewById(R.id.weight);
final EditText heightfield=(EditText) findViewById(R.id.height);
final TextView bmifield=(TextView) findViewById(R.id.bmi);
final Button button=(Button) findViewById(R.id.calc);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
try{
int num1=Integer.parseInt(weightfield.getText().toString());
float num2=Float.parseFloat(heightfield.getText().toString());

	    float ans=num1/num2*num2;
	    String S = null;
	    if(ans<19.5)
	    {
	    	S = "Oh ho!!! You are undernourished...";
	    }
	    if(ans>=19.5&&ans<=25)
	    {
	    	S = "You are appropriately nourished...";
	    }
	    if(ans>25)
	    {
	    	S = "Oh ho!!! You are overweight...";
	    }
	   
	    String str = Float.toString(ans);
	    bmifield.setText(str+S);
	} catch (Exception e)
	{
	}
}
});

}
}

@abhinavkaul95 : A better place to ask this would be www.stackoverflow.com

1 Like

Try creating another activity and printing on that.

DisplayBMIActivity

In its .java file, you will have to create an object of Intent type and there get the string passed from MainActivity

while calculating answer, use the following…
brackets are essential.

float ans=num1/(num2*num2);

i think the problem is in float ans=num1/num2*num2; This is because the compiler evaluates according to bodmas. So, the desired result is not achieved. Apart from that when I checked it and it is giving the required text in the bmi textview. By printing if you want to print it to the logcat add Log.d(“debug”,str+S); after bmifield.setText(str+S);, or use System.out.println(str+s); to print to console. Also use www.stackoverflow.com for these kind of doubts.

//