3

enter image description here

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);
        }
    }
  • 1
    please post the stack trace/ error log –  Dec 05 '15 at 08:59
  • What is the error message? Maybe you input something that is not a number – omer727 Dec 05 '15 at 09:00
  • @AndroidWeblineindia here it is https://drive.google.com/file/d/0BzRtxsi3HCNWMjNlRFMwM0hOd3M/view?usp=sharing –  Dec 05 '15 at 09:04
  • @omer727 no sir I didn't input any characters or numbers yet when the app crash –  Dec 05 '15 at 09:06
  • Your `total_score.toString()` or `editScore.getText().toString()` string returning you empty string. – Kunu Dec 05 '15 at 09:11
  • before calling this method are you checking if the edittext is empty or not. If editText is empty then please create validation. – Surender Kumar Dec 05 '15 at 09:29

5 Answers5

2

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

Khang Doan
  • 436
  • 3
  • 9
  • y has a fixed value which is 100 and x is... yeah it is still empty and the type of total_score is String –  Dec 05 '15 at 09:19
  • you can you System.out.println() function to print out two value of total_score.toString() and editScore.getText().toString(). Certainly, both are not empty string – Khang Doan Dec 05 '15 at 09:29
0

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

omer727
  • 7,117
  • 6
  • 26
  • 39
0

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 :)

0

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();
   }
}
androgo
  • 564
  • 2
  • 8
0

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();
        }

    }