Reputation: 13
I ran my code and in eclipse in the log it is showing under text the output as what my else statement is set to. I do not see this text on the screen. I am trying to compare a number to a number in my if statement. I have stated PleaseEnter as android:inputType="number" in the layout. I have included some of the ways that I have tried to change my if statement to compare the values. I need help with 1. Having if statement validated to show what it needs to and 2. Have the output show on the screen.
try
PleaseEnter=(EditText)findViewById(R.id.PleaseEnter);
{
if ("1".equals(PleaseEnter))
//(PleaseEnter.getText().equals("1"))
//(PleaseEnter.equals("1"))
{
// tv.setText("This is the display 1");
System.out.println("This is 1");
}
else if (PleaseEnter.equals("2"))
System.out.println("This 2");
else if (PleaseEnter.equals("3"))
System.out.println("This 3");
else
System.out.println("That number is not recognized.");
}}
catch (Exception ex)
Upvotes: 1
Views: 77
Reputation: 13564
PleaseEnter is an EditText, but you are testing to see if it equals a String. You should be comparing against PleaseEnter.getText()
;
An even better approach is to first convert the contents of your text field to a number, like:
Integer entered = new Integer(PleaseEnter.getText());
if (entered == 1){
//do stuff
}
else if (entered == 2){
//etc...
}
Upvotes: 0
Reputation: 18489
try this:
if ("1".equals(PleaseEnter.getText().toString().trim())) {
//your stuffs
} else .....
Upvotes: 1