【问题标题】:Error in Capitalizing first letter of string字符串首字母大写错误
【发布时间】:2021-01-15 19:49:59
【问题描述】:

A 和 B 是 2 个字符串,我们必须将每个字符串的第一个字母大写并将它们打印在一行中。我写了下面的代码

System.out.println( Character.UpperCase(A.charAt(0)) + A.substring(1)+ " " + Character.toUpperCase(B.charAt(0)) + B.substring(1));

出现以下错误:

Solution.java:21: error: cannot find symbol
        System.out.println( Character.UpperCase(A.charAt(0)) + A.substring(1)+ " " + Character.toUpperCase(B.charAt(0)) + B.substring(1));
                                     ^
  symbol:   method UpperCase(char)
  location: class Character
1 error

谁能解释我的错误是什么以及如何纠正它?

【问题讨论】:

  • Character.UpperCase 不会遵循 java 命名约定。它看起来更像是一个 c# 函数名,使用帕斯卡大小写。

标签: java string char


【解决方案1】:

Character 中没有名为UpperCase() 的方法。但是有toUpperCase()

System.out.println(Character.toUpperCase(A.charAt(0)) + A.substring(1) + " " + Character.toUpperCase(B.charAt(0)) + B.substring(1));

【讨论】:

    【解决方案2】:

    错误代码显示未找到大写符号。这是真的。

    你应该使用 Character.toUpperCase() 方法。

    【讨论】:

      【解决方案3】:

      没有这种方法UpperCase 使用下面的代码行

         System.out.println( Character.toUpperCase(A.charAt(0)) + A.substring(1)+ " " + Character.toUpperCase(B.charAt(0)) + B.substring(1));
      

      【讨论】:

        【解决方案4】:

        此外,如果您想检查(第一个字母是否已经大写)并继续..

        字符串名称 = "Manish";

            if(name.charAt(0)>96 && name.charAt(0)<123){
                System.out.println("If block called");
                System.out.println(Character.toUpperCase(name.charAt(0)) + name.substring(1));
            } else {
                System.out.println("Else block called");
                System.out.println(name);
            }
        

        【讨论】:

          猜你喜欢
          • 2016-02-15
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2011-05-12
          • 2011-01-20
          • 2021-07-29
          • 2011-05-14
          • 1970-01-01
          相关资源
          最近更新 更多