I have an input field (normal EditText ) and there is a counter for the number of characters entered in this input field.

It is necessary that when entering the Latin alphabet was added to the counter +2 (and not +1, as when entering other characters).

Should I write a check for each user character entered manually or maybe there are already ready-made solutions for such tasks?

    2 answers 2

    Should I write a check for each user character entered manually or maybe there are already ready-made solutions for such tasks?

    Ready-made solutions are most likely there, but finding them will be a problem. And why search for something, if the solution to your problem is three lines of code?

    So yes, you should write a method with similar functionality yourself.

    To solve your problem, you can use character codes:

    • Latin uppercase character codes belong to the set [65;90] ;
    • Lower case Latin character codes belong to the set [97;122] .

    The method that increases the counter according to your logic can be implemented as follows:

     private int incCounter(char c, int counter) { int charCode = (int) c; if ((c >= 65) && (c <= 90) || (c >= 97) && (c <= 122)) { return ++counter; } else { return counter+2; } } 

    In general, using the ternary operator, the code of the above method can be written in one line, but then the logic of the method will become a little less obvious and the code will be less readable.

      You can do a lot with the Latin alphabet. And check the symbol for entry into this set. This is the easiest option.