英文:
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)>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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论