我使用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()


当前回答

这段代码在文本中大写每个单词!

public String capitalizeText(String name) {
    String[] s = name.trim().toLowerCase().split("\\s+");
    name = "";
    for (String i : s){
        if(i.equals("")) return name; // or return anything you want
        name+= i.substring(0, 1).toUpperCase() + i.substring(1) + " "; // uppercase first char in words
    }
    return name.trim();
}

其他回答

试试这个

这个方法做的是,考虑单词“hello world”这个方法把它变成“hello world”每个单词的开头大写。

 private String capitalizer(String word){

        String[] words = word.split(" ");
        StringBuilder sb = new StringBuilder();
        if (words[0].length() > 0) {
            sb.append(Character.toUpperCase(words[0].charAt(0)) + words[0].subSequence(1, words[0].length()).toString().toLowerCase());
            for (int i = 1; i < words.length; i++) {
                sb.append(" ");
                sb.append(Character.toUpperCase(words[i].charAt(0)) + words[i].subSequence(1, words[i].length()).toString().toLowerCase());
            }
        }
        return  sb.toString();

    }

您可以使用以下代码:

public static String capitalizeString(String string) {

    if (string == null || string.trim().isEmpty()) {
        return string;
    }
    char c[] = string.trim().toLowerCase().toCharArray();
    c[0] = Character.toUpperCase(c[0]);

    return new String(c);

}

使用JUnit的示例测试:

@Test
public void capitalizeStringUpperCaseTest() {

    String string = "HELLO WORLD  ";

    string = capitalizeString(string);

    assertThat(string, is("Hello world"));
}

@Test
public void capitalizeStringLowerCaseTest() {

    String string = "hello world  ";

    string = capitalizeString(string);

    assertThat(string, is("Hello world"));
}
import java.util.*;
public class Program
{
    public static void main(String[] args) 
      {
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String[] s2=s1.split(" ");//***split text into words***
        ArrayList<String> l = new ArrayList<String>();//***list***
        for(String w: s2)
        l.add(w.substring(0,1).toUpperCase()+w.substring(1)); 
        //***converting 1st letter to capital and adding to list***
        StringBuilder sb = new StringBuilder();//***i used StringBuilder to convert words to text*** 
        for (String s : l)
          {
             sb.append(s);
             sb.append(" ");
          }
      System.out.println(sb.toString());//***to print output***
      }
}

我已经使用split函数将字符串分割成单词,然后我再次使用list来获得该单词的第一个字母大写,然后我使用字符串生成器以字符串格式打印输出,其中包含空格

StringUtils.capitalize(..) from commons-lang

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]+" ");
        }


    }
}