字符串首字母大写错误

huangapple go评论76阅读模式
英文:

Error in Capitalizing first letter of string

问题

A 和 B 是两个字符串,我们需要将它们的第一个字母变成大写,并将它们在同一行打印出来。我已经写了下面的代码:

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

有人能解释一下我的错误是什么,以及如何纠正吗?

英文:

A and B are 2 strings to which we have to make the 1st letters of each of them capital and print them in a single line. I have written the below code

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

The following error occurs :

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

Can someone please explain what my error is and how to correct it?

答案1

得分: 1

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

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

there is no such method UpperCase
use below line of code

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

答案2

得分: 0

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

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

There is no method called UpperCase() in Character. But there is toUpperCase().

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

答案3

得分: 0

错误代码表示未找到大写符号。这是正确的。

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

英文:

The error code says UpperCase symbol is not found. And it is true.

You should go with Character.toUpperCase() method instead.

答案4

得分: 0

此外,如果您想检查(首字母是否已为大写),然后继续执行...

String name = "Manish";

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

英文:

Moreover if you want to check(if first letter is already capital or not) and proceed..

String name = "Manish";

	if(name.charAt(0)&gt;96 &amp;&amp; name.charAt(0)&lt;123){
		System.out.println(&quot;If block called&quot;);
		System.out.println(Character.toUpperCase(name.charAt(0)) + name.substring(1));
	} else {
		System.out.println(&quot;Else block called&quot;);
		System.out.println(name);
	}

huangapple
  • 本文由 发表于 2020年9月30日 15:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定