A code to identify one letter?

Hi, I am a little programming noob when programming, but I want to create an IF statement, regardless of whether the text (or not) the text image (which I already mentioned) contains a letter in its side, and only this letter, for example, I want change any text that has "1" in it, what is the code? this is what i have, can anyone help me fill it out?

if ("!".contains(stuff.getText()) {
    stuff.setText("Incorrect Symbol");
}else {

}

      

I know I can use the keyboard to control what can be entered, but I'd rather have someone tell me how to do this. BTW, I keep getting a little red line above stuff.gettext, so can someone please tell me about the problem?

+3


source to share


5 answers


I think there are two main questions here:

  • You are a little confused about the syntax
  • Android often uses CharSequence for its text values, not String values, so it makes it a little more complicated.

Assuming "stuff" is your TextView, you can do the following:

String stuffText = stuff.getText().toString();
if(stuffText.contains("1")) {
    stuff.setText("Incorrect Symbol");
} else {

}

      



I'm not sure why you are getting a red line on stuff.getText (), but there should be a corresponding compiler error on that line that you can check in the appropriate view (assuming you are in an IDE like Eclipse).

And as far as overall design goes, that's a bad way to go. You can specify which characters the field accepts by customizing the XML:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone|numberSigned"
    android:digits="0123456789" />

      

If you really want feedback, you can use the TextWatcher so you can respond as custom types.

+4


source


Use String.contains ("yourCharacter") to check if your character is present in the string or not.

So your code will look like



if (stuff.getText().contains("!")) {
    stuff.setText("Incorrect Symbol"); // your text contains the symbol
}else {
    ..... // your text does not contain the symbol
}

      

+1


source


If you want to see if it only has "1" in it, that is, all that's entered is "1" - then you want to use equals

, not a contains.

Assuming it stuff.getText()

returns the entered text:

if("1".equals(stuff.getText())) {
    // we'll end up here if the only thing in the input is 1
} else {
    // otherwise we'll end up here
}

      

For letters, you want to use equalsIgnoreCase

for case insensitive comparisons.

If you want to check that the input contains a character, you will use the contains

equals method instead:

if(stuff.getText().contains("1")) {
    // we end up here if the input text contains 1 somewhere in it
} else {
    // otherwise we'll end up here
}

      

+1


source


if(stuff.gettext().toString().contains("!") {
    stuff.setText("Incorrect Symbol");
} else {

}

      

+1


source


You can do:

if (stuff.getText().contains("!"))

      

or

if(stuff.getText().indexOf("!") != -1)

      

Since it indexOf

will return -1

if the given char is not in the string.

As mentioned above, it indexOf

takes one character as an argument, so if you want to see if a string contains a specific substring, use contains

.

From the docs :

Returns: the index of the first occurrence of a character in the character sequence represented by this object, or -1 if no character occurs.

+1


source







All Articles