Reputation: 67
I am writing an android application to calculate the relationship between somebody`s height and weight. Everything works just fine and I managed to do the arithmetic.
However, when I want to put the result of the TextView in an if statement, no matter the result is greater or smaller or equal to, I receive the output of the first if statement. In fast, I want my program to give three different result based on the calculation. Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// My Programming Begins Here
final EditText editTextHeight = (EditText) findViewById(R.id.editTextHeight);
final EditText editTextWeight = (EditText) findViewById(R.id.editTextWeight);
Button calculateButton = (Button) findViewById(R.id.calculateButton);
final TextView result = (TextView) findViewById(R.id.result);
final TextView suggestionResult = (TextView) findViewById(R.id.suggestionResult);
// Coding for the Button
calculateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int heightValue = Integer.parseInt(editTextHeight.getText()
.toString());
int weightValue = Integer.parseInt(editTextWeight.getText()
.toString());
// Finding out the square of weightValue and then dividing the
// heightValue by the sqaure of weightValue
result.setText(String.valueOf(heightValue
/ (Math.sqrt(weightValue))));
if (result.getText().toString().length() >= 1
&& result.getText().toString().length() < 18.5) {
suggestionResult.setText("You have to gain weight");
}
if (result.getText().toString().length() >= 18.5
&& result.getText().toString().length() < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
}
});
}
}
Upvotes: 0
Views: 4274
Reputation: 11214
You should use the result of the calculation directly in the if statements. Do not put it first in a textview where you have to retrieve it to do the comparisons.
Upvotes: 0
Reputation: 48592
result.getText().toString().length() >= 18.5
You are doing wrong here. It will give the length of character inside the editext not value and this condition will never be true.
float rs = Float.parseFloat(result.getText().toString());
Then do comparison.
if (rs >= 1 && rs < 18.5f) {
suggestionResult.setText("You have to gain weight");
}
if (rs >= 18.5f rs < 24.9f) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
Upvotes: 3
Reputation: 20155
Try this
double d=Double.parseDouble(result.getText().toString());
if ( d<=18.5) {
suggestionResult.setText("You have to gain weight");
}
if (d >= 18.5 && d < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
Upvotes: 4
Reputation: 2895
Do it like this :-)
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// My Programming Begins Here
final EditText editTextHeight = (EditText) findViewById(R.id.editTextHeight);
final EditText editTextWeight = (EditText) findViewById(R.id.editTextWeight);
Button calculateButton = (Button) findViewById(R.id.calculateButton);
final TextView result = (TextView) findViewById(R.id.result);
final TextView suggestionResult = (TextView) findViewById(R.id.suggestionResult);
// Coding for the Button
calculateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int heightValue = Integer.parseInt(editTextHeight.getText()
.toString());
int weightValue = Integer.parseInt(editTextWeight.getText()
.toString());
// Finding out the square of weightValue and then dividing the
// heightValue by the sqaure of weightValue
result.setText(String.valueOf(heightValue
/ (Math.sqrt(weightValue))));
if (Integer.parseInt(result.getText().toString()) >= 1
&& result.getText().toString().length() < 18.5) {
suggestionResult.setText("You have to gain weight");
}
if (Integer.parseInt(result.getText().toString()) >= 18.5
&& result.getText().toString().length() < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
}
});
} }
Upvotes: 0
Reputation: 8747
Use this to get the value from the TextView
float f = Float.parseFloat(result.getText().toString());
Upvotes: 0
Reputation: 1491
if (Double.ValueOf(result.getText()) >= 1
&& Double.ValueOf(result.getText()) < 18.5) {
suggestionResult.setText("You have to gain weight");
}
if (Double.ValueOf(result.getText()) >= 18.5
&& Double.ValueOf(result.getText()) < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
Upvotes: 0