Having some issues with my calculation results appearing as infinity.
Basically, there is an Edittext for user input and two radio buttons. Grams and Ounces.
When the user inputs their edittext/serving size I need to run a calculation depending on radiobutton selection.
If the Grams radio button is selected I need the textview to display the result of "1000/Servingsize"
If the Ounces radio button is selected I need the text view to display the result of "(1000 / (servingsize * 28.3495) )
I have put the code for my two most recent attempts, I have tried other code but it was late last night and I cannot remember for the life of me.
Any help you can offer would be most appreciated.
Cheers, Jess.
Working Portion (Results show as expected):
package com.example.jess.dogfeedingguide;
import...
public class kCals extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
EditText thirdNumber;
EditText servingsize;
TextView addResult;
TextView addResult2;
Button btnAdd;
Button btnAdd2;
RadioButton grradio, ozradio;
double num1, num2, num3, serve, servefinalg, servefinaloz, serve2, ozinkg, sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_k_cals);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
servingsize = (EditText) findViewById(R.id.txtNumber4);
firstNumber = (EditText) findViewById(R.id.txtNumber1);
secondNumber = (EditText) findViewById(R.id.txtNumber2);
thirdNumber = (EditText) findViewById(R.id.txtNumber3);
btnAdd = (Button) findViewById(R.id.btnAdd);
addResult = (TextView) findViewById(R.id.txtResult);
grradio = (RadioButton) findViewById(R.id.radioButton1);
ozradio = (RadioButton) findViewById(R.id.radioButton2);
btnAdd2 = (Button) findViewById(R.id.btnAdd2);
addResult2 = (TextView) findViewById(R.id.txtResult2);
btnAdd.setOnClickListener(new View.OnClickListener()
// Perform kCals Per Serve Calculation //
// Check for Blank Values //
public double parseDouble(String doubleText) {
if (TextUtils.isEmpty(doubleText)) {
return 0.00;
}
try {
return Double.parseDouble(doubleText);
} catch (NumberFormatException e) {
return 0.00;
}
}
// Calculation //
public void onClick(View v) {
num1 = parseDouble(firstNumber.getText().toString());
num2 = parseDouble(secondNumber.getText().toString());
num3 = parseDouble(thirdNumber.getText().toString());
sum = (num1 * 4) + (num2 * 4) + (num3 * 9);
addResult.setText(String.format("%.2f", sum));
}
});
}
Non-Working Portion (Follows on from above portion, Result Infinity):
// Perform kCals Per Kilogram Calculation //
// Check for Blank Values //
public double parseDouble(String doubleText) {
if (TextUtils.isEmpty(doubleText)) {
return 0.00;
}
try {
return Double.parseDouble(doubleText);
} catch (NumberFormatException e) {
return 0.00;
}
}
// Calculation //
public void onRadioButtonClicked(View view) {
int kg = 1000;
ozinkg = 28.3495;
serve = parseDouble(servingsize.getText().toString());
serve2 = serve * ozinkg;
double servefinalg = (double) kg / serve;
double servefinaloz = (double) kg / serve2;
String stservefinalg = Double.toString(servefinalg);
String stservefinaloz = Double.toString(servefinaloz);
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
addResult2.setText(stservefinalg);
addResult2.setEnabled(true);
} else {
addResult2.setEnabled(false);
}
break;
case R.id.radioButton2:
if (checked) {
addResult2.setText(stservefinaloz);
addResult2.setEnabled(true);
} else {
addResult2.setEnabled(false);
}
break;}
}
}
}
I have also tried the following instead:
// Perform kCals Per Kilogram Calculation //
// Check for Blank Values //
public double parseDouble(String doubleText) {
if (TextUtils.isEmpty(doubleText)) {
return 0.00;
}
try {
return Double.parseDouble(doubleText);
} catch (NumberFormatException e) {
// do something if invalid number maybe also return 0?
return 0.00;
}
}
// Calculation //
public void onRadioButtonClicked(View view) {
ozinkg = 28.3495;
serve = parseDouble(servingsize.getText().toString());
servefinalg = 1000.00 / serve;
servefinaloz = 1000.00 / (serve * 28.3495);
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked) {
addResult2.setText(String.format("%.2f", servefinalg));
addResult2.setEnabled(true);
} else {
addResult2.setEnabled(false);
}
break;
case R.id.radioButton2:
if (checked) {
addResult2.setText(String.format("%.2f", servefinaloz));
addResult2.setEnabled(true);
} else {
addResult2.setEnabled(false);
}
break;
}
}
}
serve = parseDouble(servingsize.getText().toString());
is what is more than likely causing the problem.
Main way that you can get infinity returned is by dividing by zero.
public double parseDouble(String doubleText) {
if (TextUtils.isEmpty(doubleText)) {
return 0.00;
}
try {
return Double.parseDouble(doubleText);
} catch (NumberFormatException e) {
// do something if invalid number maybe also return 0?
return 0.00;
}
}
is returning zero on the case that an exception is thrown or the input is empty.
Because text is a string of characters, you need something to parse into a double/integer.
On the case where it is either empty or throws an exception (which is what is happening, because it is a string of characters, not a number), it returns zero.
servefinalg = 1000.00 / serve;
is dividing by serve, which is set by serve = parseDouble(servingsize.getText().toString());
.
It is being assigned to zero by your parseDouble method, then you divide by zero.
My solution would be to instead use multiplication, change your divisor to a fraction, so then if there's an invalid input it gets set to zero rather than infinity.