skip to Main Content

I want to make a quizscore calculating app. I have some problems. How to I prevent entering value greater than 20 inside the edittext and if the edittext is empty, write 0 inside the edittext and calculate the 0 value. When the application runs, it calculates the score, but when the Edittext is entered null, the application closes. I want to if edittext is null, app sets the null value 0 and calculate score.
Here’s the image my app:1: https://i.stack.imgur.com/efn9H.png

Here’s the code I’m using:

public class MainActivity extends AppCompatActivity {

EditText editText1,editText2,editText3;
private Button calculate;
TextView textView1;
double c=0.0,x;
int a=0,b=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText1=findViewById(R.id.correct_answer);
    editText2=findViewById(R.id.false_answer);
    editText3=findViewById(R.id.turnet);
    textView1=findViewById(R.id.hesap);
    calculate=findViewById(R.id.calculate);

    calculate.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            a=Integer.parseInt(editText1.getText().toString());
            b=Integer.parseInt(editText2.getText().toString());
            x=a+b;
            if (x<=20){
                if (editText1.getText().toString().length()==0)
                {
                    a=0;
                    Toast.makeText(getApplicationContext(),"Correct Answers must be max:20 .", Toast.LENGTH_LONG).show();
                }
                else{
                    a=Integer.parseInt(editText1.getText().toString());
                }
                if (editText2.getText().toString().length()==0)
                {
                    Toast.makeText(getApplicationContext(),"False Answers must be max:20.", Toast.LENGTH_LONG).show();
                    b=0;
                    editText2.setText(0);
                }

                double c = a - (b / 3);
                double result=((200.004394)+(c*3.44603333));
                editText3.setText(Double.toString(c));
                textView1.setText("Your Score: "+ Double.toString(result));

                    }
                else{
                Toast.makeText(getApplicationContext(),"All English questions dont greater than 20.", Toast.LENGTH_LONG).show();
                }

    };});}}

2

Answers


  1. You can use TextWatcher. Write a TextWatcher which change value to 20 if it is greater than 20 or set it to 0 if EditText‘s text becomes empty

    Login or Signup to reply.
  2. There is a problem with this code :

    a=Integer.parseInt(editText1.getText().toString());
    b=Integer.parseInt(editText2.getText().toString());
    

    when one of your edittextes is empty, then an error will be thrown because Integer.parseInt cannot accept an empty string.
    so use this:

    a = 0;
    b = 0;
    if (editText1.getText().toString() != ""){
        a=Integer.parseInt(editText1.getText().toString());
    }
    if(editText2.getText().toString() != ""){
        b=Integer.parseInt(editText2.getText().toString());
    }
    x = a + b
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search