This is a OnClickListener for button
name is EditText
I want it print only "hi" if nothing is entered, but "hi" + name + "!" if user inputs his/her name.
public void onClick(View view) {
if (button==view) {
String message;
if (name.getText().toString().matches("")) {
message = "hi!";
return;
}
else {
message = "hi" + name.getText().toString() + "!";
return;
}
Toast toast = Toast.makeText(this,message,Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
For some reason I got error "unreachable statement" for the line:
Toast toast =...,
And if I compile and run it, the screen will output on 2 lines instead of one, such as:
Hi
!
What did I do wrong in here?