1

I'm trying to pop a message that the EditText is empty. I can't make it work. I guess it's trying to convert an Integer to a String.

Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(21);


public void buttonPressed (View view){
    System.out.println(randomNumber);

    EditText numberField = (EditText)findViewById(R.id.textField);
    String numberString = numberField.getText().toString();
    int numberText = Integer.parseInt(numberString);
    String message = "";

    if (randomNumber == numberText){
        message = "Correct Number";

    }
    else if (randomNumber > numberText){
        message = "Number too Low";

    }
    else if (randomNumber < numberText){
        message = "Number too High";
    }
    else if(numberString.matches("")){
        Toast.makeText(getApplicationContext(), "Enter a Number!", Toast.LENGTH_LONG).show();
        return;
    }

    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • 3
    *I can't make it work* => any error message ? I would guess the same as you based on the above, you probably have a `NumberFormatException` that crashes your app... – 2Dee Jan 12 '16 at 17:40
  • It is probably best that you remove `I'm new with Android Programming. Please help me with my code.` from your question and insert the error message you are getting instead. – Olga Jan 12 '16 at 17:43
  • Thanks for accepting my answer ! See my edit about limiting the user input to numbers. Please do take the time to go through the accepted answer for the question I link to as well, it will teach you how to effectively debug the app on your own, thus gaining productivity and experience at the same time. Happy coding ;) – 2Dee Jan 12 '16 at 18:00

4 Answers4

1

You are trying to parse an integer even if the field is empty which will throw a NumberFormatException. You will need to handle the case where the user does not enter a valid number. To get started though try the following:

public void buttonPressed (View view) {
    System.out.println(randomNumber);

    EditText numberField = (EditText)findViewById(R.id.textField);
    String numberString = numberField.getText().toString();

    if(TextUtils.isEmpty(numberString)) {
        Toast.makeText(getApplicationContext(), "Enter a Number!", Toast.LENGTH_LONG).show();
    } else {
        // FIXME: add code to handle invalid integer here
        int numberText = Integer.parseInt(numberString);
        String message = "";

        if (randomNumber == numberText){
            message = "Correct Number";
        }
        else if (randomNumber > numberText){
            message = "Number too Low";
        }
        else if (randomNumber < numberText){
            message = "Number too High";
        }

        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
1

If your App crashes :

Take a look at this question

If the crash is a NumberFormatException :

encapsulate your call to parseInt in a try/catch block and check if the EditText is empty first :

if (numberString.isEmpty()){
    Toast.makeText(getApplicationContext(), "Enter a Number!", Toast.LENGTH_LONG).show();
}else{
    try {
        int numberText = Integer.parseInt(numberString);
        // the rest of your code
    } catch (NumberFormatException e) {
        Log.e(getClass.getSimpleName(), e);
        Toast.makeText(getApplicationContext(), "Enter a Number!", Toast.LENGTH_LONG).show();
    } 
}

EDIT : to ensure the user can only insert a number, you can use the inputType property of EditText :

<EditText
    android:id="@+id/number_edit_text"
    style="@style/yourStyle"
    android:inputType="number"/>
Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
0

try this:

if(numberString.equals(""))
   {
        Toast.makeText(getApplicationContext(), "Enter a Number!",  Toast.LENGTH_LONG).show();
        return;
   }

Hope it helps.

Kristo
  • 1,339
  • 12
  • 22
0

First of all, I would put that as the first thing, because if you have an empty value, then you will get a NumberFormatException when converting, and as a result, it will crash. Move your check for that to before the parseInt command.

String numberString = numberField.getText().toString();
if(numberString.matches("")){

        Toast.makeText(getApplicationContext(), "Enter a Number!", Toast.LENGTH_LONG).show();
        return;
    }
int numberText = Integer.parseInt(numberString);

Even better would be to put the parseInt inside of a try/catch, and then do the number checks.

try {
    int numberText = Integer.parseInt(numberString);
    // Put checks in here to see if the number is right
} catch (NumberException ex) {
        Toast.makeText(getApplicationContext(), "Invalid entry detected", Toast.LENGTH_LONG).show();
        return;
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142