user3287264
user3287264

Reputation:

Android: Adding validation to ensure a user can only enter numbers

In my app I am taking answers from a user for a mathematical game. Once the answer is entered the next question is displayed etc. Currently if the question is answered and a letter or a blank answer is entered by mistake the app crashes. I want for the app to just not take the user's answer if it is invalid and to keep displaying the question until a valid answer (number) is given.

Current code:

// sets text view equal to what is entered in editText
        final String entry = answer.getText().toString();
        // convert from string value to int
        int a = Integer.parseInt(entry); //

        // setting the user answer equal to the correct part of results array
        results[questionNumber - 1] = a;

        // If user answer is equal to correct answer then increase score
        if (a == correctAnswer[questionNumber - 1]) {
            score++;
            correctNoise.start();
            imageRandom.setImageResource(R.drawable.thumbsup);
        }else{

            incorrectNoise.start();
            imageRandom.setImageResource(R.drawable.thumbsdown);

        }

Upvotes: 0

Views: 97

Answers (5)

Phil
Phil

Reputation: 36299

You should enforce the InputType of the EditText. This will make the numbers-only keyboard display. For example, to do this in XML:

android:inputType="numberSigned"

or from code:

answer.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED);

Finally, just make sure the input is not empty:

CharSequence seq = answer.getText();
if (seq == null)
    return;
String s = seq.toString().trim();
if (s.length() == 0)
    return;
int a = Integer.parseInt(s);
//TODO use a, the valid integer response.

Upvotes: 1

Alireza
Alireza

Reputation: 105

to ensure that user entered number first you can use

answer.setinputtype(inputtype.number);

and the second to write a function to check the value is number

public static boolean tryParseInt(String value )  
    {  
         try  
         {  
             Integer.parseInt(value);  
             return true;  
          } catch(NumberFormatException nfe)  
          {  
              return false;  
          }  
    }

Upvotes: -1

Pradeep Kumar
Pradeep Kumar

Reputation: 877

// sets text view equal to what is entered in editText

    if(answer.getText()!=null) {
    final String entry = answer.getText().toString();
    // convert from string value to int
    int a = Integer.parseInt(entry); //

    // setting the user answer equal to the correct part of results array
    results[questionNumber - 1] = a;

    // If user answer is equal to correct answer then increase score
    if (a == correctAnswer[questionNumber - 1]) {
        score++;
        correctNoise.start();
        imageRandom.setImageResource(R.drawable.thumbsup);
    }else{

        incorrectNoise.start();
        imageRandom.setImageResource(R.drawable.thumbsdown);

    }
}

Upvotes: -1

Craig T
Craig T

Reputation: 11

Integer.parseInt() can throw a NumberFormatException.

You might try wrapping it like this, and then handling the exception by rejecting the user's answer:

try {
    int a = Integer.parseInt(entry);
} catch(NumberFormatException ex) {
    // do something to handle the error
}

You can also ask the Android keyboard to show numbers using:

android:inputType="number"

on your EditText

Upvotes: 1

VM4
VM4

Reputation: 6499

Just add android:inputType="number" to your EditText xml View. If you want to check it in code, this will check if a String is numeric:

public static boolean isNumeric(String str)  
{  
  try  
  {  
    double d = Double.parseDouble(str);  
  }  
  catch(NumberFormatException nfe)  
  {  
    return false;  
  }  
  return true;  
}

Source: How to check if a String is numeric in Java

Upvotes: 4

Related Questions