我使用Java从用户获得一个字符串输入。我试着让输入的第一个字母大写。

我试了一下:

String name;

BufferedReader br = new InputStreamReader(System.in);

String s1 = name.charAt(0).toUppercase());

System.out.println(s1 + name.substring(1));

这导致了以下编译错误:

类型不匹配:不能从InputStreamReader转换为BufferedReader 不能在基本类型char上调用toUppercase()


当前回答

在Android Studio中

将此依赖项添加到构建中。gradle(模块:app)

dependencies {
    ...
    compile 'org.apache.commons:commons-lang3:3.1'
    ...
}

现在你可以使用

String string = "STRING WITH ALL CAPPS AND SPACES";

string = string.toLowerCase(); // Make all lowercase if you have caps

someTextView.setText(WordUtils.capitalize(string));

其他回答

这只是为了告诉你,你没有错。

BufferedReader br = new InputstreamReader(System.in);
// Assuming name is not blank
String name = br.readLine(); 

//No more error telling that you cant convert char to string
String s1 = (""+name.charAt(0)).toUppercase());
// Or, as Carlos prefers. See the comments to this post.
String s1 = Character.toString(name.charAt(0)).toUppercase());

System.out.println(s1+name.substring(1));

注意:这根本不是最好的方法。这只是为了告诉OP,它也可以使用charAt()来完成。;)

你要做的可能是:

s1 = name.substring(0, 1).toUpperCase() + name.substring(1);

(将第一个字符转换为大写,并添加原始字符串的其余部分)

此外,您创建了一个输入流读取器,但从不读取任何行。因此name将始终为空。

这应该可以工作:

BufferedReader br = new InputstreamReader(System.in);
String name = br.readLine();
String s1 = name.substring(0, 1).toUpperCase() + name.substring(1);

以下是我关于Android中所有可能的选项的详细文章

Java中字符串首字母大写的方法

public static String capitalizeString(String str) {
        String retStr = str;
        try { // We can face index out of bound exception if the string is null
            retStr = str.substring(0, 1).toUpperCase() + str.substring(1);
        }catch (Exception e){}
        return retStr;
}

在KOTLIN中首字母大写的方法

fun capitalizeString(str: String): String {
        var retStr = str
        try { // We can face index out of bound exception if the string is null
            retStr = str.substring(0, 1).toUpperCase() + str.substring(1)
        } catch (e: Exception) {
        }
        return retStr
}
class CapitalizeWords
{
    public static void main(String[] args) 
    {
        String input ="welcome to kashmiri geeks...";

        System.out.println(input);

        String[] str = input.split(" ");

        for(int i=0; i< str.length; i++)
        {
            str[i] = (str[i]).substring(0,1).toUpperCase() + (str[i]).substring(1);
        }

        for(int i=0;i<str.length;i++)
        {
            System.out.print(str[i]+" ");
        }


    }
}

使用Apache的公共库。把你的大脑从这些东西中解放出来,避免空指针和索引脱离绑定异常

步骤1:

在build中导入apache的common lang库。gradle依赖性

implementation 'org.apache.commons:commons-lang3:3.6'

步骤2:

如果你确定你的字符串都是小写的,或者你只需要初始化第一个字母,直接调用

StringUtils.capitalize(yourString);

如果你想确保只有第一个字母是大写的,就像在枚举中那样,首先调用toLowerCase(),并记住,如果输入字符串为空,它将抛出NullPointerException。

StringUtils.capitalize(YourEnum.STUFF.name().toLowerCase());
StringUtils.capitalize(yourString.toLowerCase());

以下是apache提供的更多示例。它是无异常的

StringUtils.capitalize(null)  = null
StringUtils.capitalize("")    = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
StringUtils.capitalize("'cat'") = "'cat'"

注意:

WordUtils也包含在这个库中,但已弃用。请不要用这个。