2

Cannot understand why when user input bachelor format like "B.Com".

I want to get "B.com" - c letter is lower case.

But I'm getting: "B.Com" - C letter is capital

Here is my code:

InputFilter value = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned spanned, int dstart, int dend) {
        for(int i = start; i < end; i++) {
            String letterOne = source.subSequence(0,1).toString().toUpperCase();
            if(Character.toString(source.charAt(i)).matches("[a-zA-Z.? ]*")) {
                return source.subSequence(0,1).toString().toUpperCase()+source.subSequence(1,end).toString().toLowerCase();
            }
            return letterOne+source.subSequence(1,end).toString().toLowerCase();
        }
        return null;
    }
};
Vladislav Varslavans
  • 2,775
  • 4
  • 18
  • 33
Michael Fernando
  • 117
  • 1
  • 10
  • Does this answer your question? [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – snnsnn Sep 14 '20 at 12:40

1 Answers1

1

your current code in for statement may be reduced

String letterOne = source.subSequence(0,1).toString().toUpperCase();
if(Character.toString(source.charAt(i)).matches("[a-zA-Z.? ]*"))
{
    return source.subSequence(0,1).toString().toUpperCase()+source.subSequence(1,end).toString().toLowerCase();
}
return letterOne+source.subSequence(1,end).toString().toLowerCase();
            

inside if statement you have source.subSequence(0,1).toString().toUpperCase(), which is exacly same as letterOne, so we can reduce some code

String letterOne = source.subSequence(0,1).toString().toUpperCase();
if(Character.toString(source.charAt(i)).matches("[a-zA-Z.? ]*"))
{
    return letterOne+source.subSequence(1,end).toString().toLowerCase();
}
return letterOne+source.subSequence(1,end).toString().toLowerCase();

now it is clearly visible that if statement contains same return as last return in method, so it may be reduced again to:

String letterOne = source.subSequence(0,1).toString().toUpperCase();
return letterOne + source.subSequence(1,end).toString().toLowerCase();

in above code you are not using i at all, so whole for statement is useless and filter may by shorted to:

@Override
public CharSequence filter(CharSequence source, int start, int end,
        Spanned spanned, int dstart, int dend) {
    if (start <= end) return null;
    String letterOne = source.subSequence(0,1).toString().toUpperCase();
    return letterOne + source.subSequence(1,end).toString().toLowerCase();
}

there is no .-present checking code, no uppercasing any other letter than first... you are always returning same String as source with first letter uppercased and all other lowercased

you may try to use indexOf method for checking if dot is present in whole String sourceAsString (convert from CharSequence source), if yes then use split method for making array with two Strings - make first letter uppercase in both words and join them String properlyFormatted = firstWord + "." + secondWord;

PS. be aware that split method takes REGEX as param and "." in REGEX means "any letter", so use escaping like this: split("\\.")

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • i have to try it before "there is no...". but, it can' effective when edittext have a value and user want to delete a word before – Michael Fernando Sep 14 '20 at 09:15
  • what do you want to try "before 'there is no...'"? this is same code as yours, works exacly the same... (so: wrong, logic problem, not in code) also what do you mean by 'it can' effective when' - thats not in english for shure... – snachmsm Sep 14 '20 at 09:23
  • i'm sorry about my grammar. but, after i try not to use for in source.charAt(i). it will be crash if in edittext have a value. for example in edittext have a value "Michael Fernando". and if i want change "Michael Setiawan" it can't because it need delete "Fernando". and then i don't get it how to implement indexOf in if condition. can you have explain more clearly about check dots value from inputFilter? – Michael Fernando Sep 14 '20 at 09:27
  • `boolean sourceContainsDot = ((String) source).indexOf(".") >= 0;` - thats how you may check that `source` contains dot. I honestly don't get what do you want to achieve and what results you are expecting to be returned by `filter` method... – snachmsm Sep 14 '20 at 14:22
  • i see. i can try it. thank you for help me about my issue. – Michael Fernando Sep 14 '20 at 16:03
  • consider upvoting/accepting my answer if helpful :) good luck! – snachmsm Sep 14 '20 at 17:32