I am using Java to get a String input from the user. I am trying to make the first letter of this input capitalized.
I tried this:
String name;
BufferedReader br = new InputStreamReader(System.in);
String s1 = name.charAt(0).toUppercase());
System.out.println(s1 + name.substring(1));
which led to these compiler errors:
Type mismatch: cannot convert from InputStreamReader to BufferedReader
Cannot invoke toUppercase() on the primitive type char
解决方案
Use Apache's common library. Free your brain from these stuffs and avoid Null Pointer & Index Out Of Bound Exceptions
Step 1:
Import apache's common lang library by putting this in build.gradle dependencies
compile 'org.apache.commons:commons-lang3:3.6'
Step 2:
If you are sure that your string is all lower case, or all you need is to initialize the first letter, directly call
StringUtils.capitalize(yourString);
If you want to make sure that only the first letter is capitalized, like doing this for an enum, call toLowerCase() first and keep in mind that it will throw NullPointerException if the input string is null.
StringUtils.capitalize(YourEnum.STUFF.name().toLowerCase());
StringUtils.capitalize(yourString.toLowerCase());
Here are more samples provided by apache. it's exception free
StringUtils.capitalize(null) = null
StringUtils.capitalize("") = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
StringUtils.capitalize("'cat'") = "'cat'"
Note:
WordUtils is also included in this library, but is deprecated. Please do not use that.