Reputation:
hello guys I am having this problem. When I enter a score in the editText, I want the app to generate the equivalent in the Textview(w/ red box). But the app crashes with this code.
private void calculateEquivalent(){
double x , y;
y = Double.valueOf(total_score.toString());
x = Double.valueOf(editScore.getText().toString());
if (x >= y * 0.65){
double equivalent = (Math.round((100 + (72 * (x - y)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
} else {
double equivalent = (Math.round((75 + (23 * (x - y * 0.65)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
}
}
Upvotes: 3
Views: 119
Reputation:
Thanks a lot guys without your suggestions I probably stuck in this problem :) and then I've come up with this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_student_quiz);
TextWatcher inputTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
calculateEquivalent();
}
};
editScore.addTextChangedListener(inputTextWatcher);
}
private void calculateEquivalent(){
try {
y = Double.parseDouble(total_score);
x = Double.parseDouble(editScore.getText().toString());
if (x >= y * 0.65){
double equivalent = (Math.round((100 + (72 * (x - y)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
} else {
double equivalent = (Math.round((75 + (23 * (x - y * 0.65)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
}
}catch (Exception e){
Toast.makeText(getApplicationContext(), "Please Enter a Number", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 574
Hey @callmejeo the main problem with the function you wrote above is that you are coverting "NULL" values into String so one thing you can do is that to HANDLE exception.
private void calculateEquivalent(){
try{
double x , y;
y = Double.valueOf(total_score.toString());
x = Double.valueOf(editScore.getText().toString());
if (x >= y * 0.65){
double equivalent = (Math.round((100 + (72 * (x - y)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
} else {
double equivalent = (Math.round((75 + (23 * (x - y * 0.65)) / y)));
String equi = String.valueOf(equivalent);
textEquivalent.setText(equi);
}
}
catch(Exception e){
Toast.makeText(this,"Please Enter some value",Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Reputation:
As the error log suggests, you need to make sure that you have proper values before you start the calculation.
So before calling this function you need to check for below conditions:
try
{
if((total_score.toString() != null && !total_score.toString().isEmpty()) && (editScore.getText().toString()!=null && !editScore.getText().toString().isEmpty()))
{
y = Double.valueOf(total_score.toString());
x = Double.valueOf(editScore.getText().toString()); //chances of getting a Numberformat exception if entered value is not a number
calculateEquivalent();
}
}
catch(NumberFormatException e)
{
//Toast.makeText(m_context, "You entered a wrong value,Please enter only numeric values", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch(Throwable e)
{
e.printStackTrace();
}
Also in your calculateEquivalent(); method, you need to make sure that value of y should not be zero.
Hope this helps you :)
Upvotes: 0
Reputation: 7565
You have a problem when trying to convert empty string to a double. You should check first that the text field is not empty and that it doesn't contain characters by catching NumberFormatException
Upvotes: 0
Reputation: 436
The error is empty string when convert from string to double
In this code
y = Double.valueOf(total_score.toString());
x = Double.valueOf(editScore.getText().toString());
May be total_score.toString() or editScore.getText().toString() was empty
And what is type of total_score variable
Upvotes: 2