Still a beginner in Android App Development. I'm trying to build a simple app that will show score grades to the user correspondingly to the score number they have entered in EditText. The problem is my app crashes when the user clicks the button without entering any text. I have tried using isEmpty and getText().toString.equals("") to solve this problem, but to no avail. What do I need to do? Please Help.
int score;
public void checkResults (View view){
Log.i("Results","Button Tapped");
Button button = (Button) findViewById(R.id.button);
EditText editTextNumber = (EditText) findViewById(R.id.editTextNumber);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
score = Integer.valueOf(editTextNumber.getText().toString());
if (editTextNumber.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter at least one number", Toast.LENGTH_SHORT).show();
}
if(score < 29){
Toast.makeText(getApplicationContext(), "You failed", Toast.LENGTH_SHORT).show();
}
else if (score >= 30 && score <= 39){
Toast.makeText(getApplicationContext(), "You got F", Toast.LENGTH_SHORT).show();
} else if (score >= 40 && score <= 49){
Toast.makeText(getApplicationContext(), "You got E", Toast.LENGTH_SHORT).show();
} else if (score >= 50 && score <= 59){
Toast.makeText(getApplicationContext(), "You got D", Toast.LENGTH_SHORT).show();
} else if (score >= 60 && score <= 69){
Toast.makeText(getApplicationContext(), "You got C", Toast.LENGTH_SHORT).show();
} else if (score >= 70 && score <= 79){
Toast.makeText(getApplicationContext(), "You got B", Toast.LENGTH_SHORT).show();
} else if (score >= 80 && score <= 100){
Toast.makeText(getApplicationContext(), "You got A", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Invalid input", Toast.LENGTH_SHORT).show();
}
}
});
}