0

I can't for the life of me figure out what I'm doing wrong. I am trying to limit the user input to a integer. So that when I use the following code

int userNumber = Integer.parseInt(userGuess.getText().toString());

my app doesn't crash.

I figured out how to do it as follows in java, http://www.browxy.com/SavedCode/48384. The only issue I'm having is transposing this concept to my app. Android Studio is not recognizing the hasNextInt() when I use it.

First I use the findViewId method to obtain the view:

EditText userGuess = (EditText) findViewById(R.id.userGuess);

Then I tried to check whether the userGuess.getText().toString(); was an integer through the means of loops. And as I said android Studio does not recognize the hasNextInt();

And when looking through the stackOverflow one of the users said to avoid using the expection to solve this problem. Please Help!

Manuty
  • 27
  • 1
  • 1
  • 7

1 Answers1

2

You can use input type number on the EditText.

<EditText
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:inputType="number" />

I'm not sure if the user is still able to copy-paste non-digit characters into this field.

http://developer.android.com/training/keyboard-input/style.html http://developer.android.com/guide/topics/ui/controls/text.html#Keyboard


The other way to check is after the user has entered the input (e.g. still needed if they can still paste non-digits in the field):

private boolean isDigit(String text) {
    try {
        Integer.parseInt(text);
        return true;
    } catch (NumberFormatException e) {
        // log warning, track analytics if you want
    }
    return false;
}

I avoid writing a method that will return the integer because it would use exceptions for control-flow. In that case, I'm unsure what number the method would return.

ataulm
  • 15,195
  • 7
  • 50
  • 92
  • This is only the first part of the solution. Please add the options above and an Textwatcher, like in http://stackoverflow.com/questions/8699569/implementing-text-watcher-for-edittext – Tobias S Oct 31 '15 at 16:05
  • I'm having the user click a button to input the text in the text edit, so I don't need the TextWatcher right? – Manuty Oct 31 '15 at 16:10
  • @TobiasK. which "options above" do you mean? Manuty, the TextWatcher would allow you continuously validate the input each time the text changes - it's up to you whether this is what you want, or whether you want to validate solely on submit. – ataulm Oct 31 '15 at 16:11
  • Oooh I see so it's a way to anticipate the error coming before it has been submitted, so we can warn the user before. Thank a lot ;) – Manuty Oct 31 '15 at 16:20
  • I'm brand new to this am I can't figure how to implement the is digit method where do I place this? Because my code is inside the click class I have created. – Manuty Oct 31 '15 at 18:20
  • You should put it in the same class that you are currently using to validate the input as this is part of the input validation. Then consider extracting this functionality into a validator. Another option is using an InputFilter - http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android – ataulm Oct 31 '15 at 19:04
  • So in this case I will not be using the private boolean correct? Because as far as I'm concerned I cannot put this inside a public class I have created for the click method. – Manuty Oct 31 '15 at 19:07
  • this sounds like it's venturing into a new question. – ataulm Oct 31 '15 at 19:10