简体   繁体   中英

Error in Capitalizing first letter of string

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?

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

   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));

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

You should go with Character. to UpperCase() method instead.

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM