1

I am making a math app. I have two arrays with two sets of numbers. I make the app choose a random number from each array. The app displays that two numbers in a TextView. It asks the user to type in a EditText what the addition of the numbers is. Then when the user presses "Submit", the app will then tell the user if they are right or wrong.

The app works, but I have one main problem. If the user does not type anything in the EditText and presses "Submit", the app force closes. I have looked at other questions asking how to check if a EditText is empty. I tried different methods but it still does not work.

public class MainActivity extends Activity {
Button submitb, startb, idkb;
EditText et;
TextView tv, rig, wron;

int arrone[] = { 24, 54, 78, 96, 48, 65, 32, 45, 95, 13, 41, 55, 33, 22,
        71, 5, 22, 17, 13, 29, 63, 72, 14, 81, 7 }; // One Set of Numbers//
int arrtwo[] = { 121, 132, 147, 79, 82, 723, 324, 751, 423, 454, 448,
        524, 512, 852, 845, 485, 775, 186, 99 }; // Another set of Numbers//
int random1, random2, add;
int wrong = 0;
int right = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list();
    startb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int num = (int) (Math.random() * 25);
            int mun = (int) (Math.random() * 19);
            random1 = arrone[num];
            random2 = arrtwo[mun];
            String yu = (random1 + " + " + random2 + " =");
            tv.setText(yu);
            et.setHint("");
            idkb.setVisibility(0);
            startb.setText("Next Question");

        }
    });

    startb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int bread2 = 0;
            if (bread2 == 0) {
                Toast.makeText(getApplicationContext(),
                        "You didn't answer the question!",
                        Toast.LENGTH_LONG).show();

            }

            int swag = random1 + random2;
            String bread = et.getText().toString();
            bread2 = Integer.parseInt(bread);

            if (bread2 == swag) {
                startb.setText("Next Question");
                tv.setText("Correct");
                et.setText("");
                et.setHint("Click Next Question");
                right++;
                rig.setText("Right:" + right);
                rig.setTextColor(Color.parseColor("#14df11"));
                bread2 = 0;
            }

            else if (bread2 == 0) {
                tv.setText("Wrong, the correct answer is " + swag + ".");

                startb.setText("Next Question");
                et.setText("");
                tv.setHint("Click Next Question");
                wrong++;
                wron.setText("Wrong:" + wrong);
                wron.setTextColor(Color.parseColor("#ff0000"));
                bread2 = 0;
            }

            else {
                tv.setText("Wrong, the correct answer is " + swag + ".");

                startb.setText("Next Question");
                et.setText("");
                et.setHint("Click Next Question");
                wrong++;
                wron.setText("Wrong:" + wrong);
                wron.setTextColor(Color.parseColor("#ff0000"));
                bread2 = 0;

            }

        }

    });

    idkb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int swag = random1 + random2;

            tv.setText("The Correct Answer is  " + swag + ".");

            et.setText("");
            tv.setHint("Click Next Question");
            wrong++;
            wron.setText("Wrong:" + wrong);
            wron.setTextColor(Color.parseColor("#ff0000"));

        }
    });

}

private void list() {
    submitb = (Button) findViewById(R.id.b1);
    startb = (Button) findViewById(R.id.b2);
    et = (EditText) findViewById(R.id.et1);
    tv = (TextView) findViewById(R.id.tv1);
    rig = (TextView) findViewById(R.id.rtv);
    wron = (TextView) findViewById(R.id.wtv);
    idkb = (Button) findViewById(R.id.idk);

}
3
  • Include the stack trace from the close Commented Feb 26, 2014 at 19:47
  • Have you added any logic to check the null or empty condition. Post that as well. Commented Feb 26, 2014 at 19:53
  • nobody used trim() to remove space from ends ???? Commented Feb 26, 2014 at 20:22

5 Answers 5

2

Few points to consider:

  • you are setting OnClickListener on startb twice, is it just a typo?
  • in the second onClick(), you have a condition:

    int bread2 = 0;
    if (bread2 == 0) {
        //rest of code
    }
    

This condition will ALWAYS be satisfied, is that what you want?

  • you can check if there's nothing entered by use with the following:

    String bread = et.getText().toString();
    
    // check if there is any text entered by user
    if (bread.length() > 0) {
        bread2 = Integer.parseInt(bread);
    
        if (bread2 == swag) {
            startb.setText("Next Question");
    
            //the rest of the code
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure I'm understanding you correctly, but it should be easy as:

et = (EditText) findViewById(R.id.et1);
String bread = et.getText().toString();
if (bread.length() == 0){
   // raise error dialogue
} else {
   // continue to check the parsed integer
}

This should all be done within your click listener.

Comments

0

You need to use findViewById on the et before gets its value.

By the way, you are setting bread2 value to 0, and on the next line verifying if it's 0, i suppose it's just a test, isn't it?

    @Override
    public void onClick(View v) {

        int bread2 = 0;
        if (bread2 == 0) {
            Toast.makeText(getApplicationContext(),
                    "You didn't answer the question!",
                    Toast.LENGTH_LONG).show();

        }

        int swag = random1 + random2;

        et = findViewById(R.id.yourEditText);

        String bread = et.getText().toString();
        bread2 = Integer.parseInt(bread);

        //...

Hope it helps!

Comments

0

I think it good practice to check text length, for example this function can help you..

 public boolean isEditTextEmpty(EditText input){
   if(input.getText().length() == 0)
      return true;
   else
      return false;
  }

Comments

0
    if (et.getText().length == 0 || et.gettext == null ){
        // do nothing
   } else {
       // do whatever here
   }

this should works fine

1 Comment

You should first check for null otherwise you will get a NPE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.