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.