user3287264
user3287264

Reputation:

Android: Dealing with blank/invalid entry?

In the Following code I am taking an answer from a user for a mathematical question. When the answer is entered the question then updates to a new one.

How would I add validation so that when a user enters letters or hits submit without entering an answer, the question just stays the same and allows the user to enter again. At the minute, when that happens the app crashes. (note: Main functionality I am refering to occurs in onClick).

public class PracticeTest extends Activity implements View.OnClickListener{
    //declare vars
    int multiplier;
    int[] results=new int[12];
    int numberPassed;
    TextView question;
    EditText answer;
    int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.practicetest);

        // This declares and int variable, assigns it an int value from the
        // calling Intent if its not there it is defaulted to 0
        numberPassed = getIntent().getIntExtra("convertedNumber2", 0);

        //setting up vars(possibly do this in different method?     
        Button submit = (Button) findViewById(R.id.btnGoPractice2); //declared here as it is only used once
        answer = (EditText) findViewById(R.id.etEnterNumberPractice2);
        question = (TextView) findViewById(R.id.tvTopPractice2);

        //setting listeners
        submit.setOnClickListener(this);

        updateQuestion();
    }

    public void onClick(View view) {
        // sets text view equal to whats typed in in editText
        final String entry = answer.getText().toString();

        // convert from string value to int
        int a = Integer.parseInt(entry); //note: maybe change name

        results[multiplier-1]=a;
        score++;//Irrelevant?

        if(multiplier<12){  
           //called after an answer is given
           updateQuestion();
        } else{
            //System.out.println(score);
            Intent intent = new Intent(this, Results.class);
            intent.putExtra("results", results);
            intent.putExtra("numberPassed", numberPassed);
            this.startActivity(intent);
        }   
    }

    public void updateQuestion(){       
        multiplier++;

        //string to hold quest
        String q= numberPassed + "x" + multiplier + "=";
        question.setText(q);
        answer.setText("");
    }
}

Upvotes: 0

Views: 52

Answers (1)

martijnn2008
martijnn2008

Reputation: 3640

So entry is the answer you get? Maybe try regex, you can use this code after submitting the answer or you could check this when a user edits the EditText. The last thing can be done with a TextWatcher, but that would make it a bit more complicated than necessary.

if(entry.matches("[0-9]+") {
  // new question
} else {
  // warning no valid answer
}

If you want your users only have the option to input numbers. You should set in your EditText:

android:inputType="number"

Upvotes: 1

Related Questions