简体   繁体   中英

Java toUppercase() method is undefined for type String

So I am fairly new at java and working my way through the exercises at coding bat, but I've stumbled on an error that I can't seem to solve. My code is:

public String endUp(String str) {
  int l = str.length();
  if (l < 4) {
    str = str.toUppercase();
    return str;
  }else{
    String strup = str.substring(l-3);
    strup = strup.toUppercase();
    return (str.substring(0,l-3) + strup);
  }
}

And I get this error:

Compile problems:
Error:  str = str.toUppercase();
              ^^^^^^^^^^^
The method toUppercase() is undefined for the type String

After doing some research, it looks like this problem mostly happens when people create a custom class (I don't know anything about java classes yet) and don't import the method into it, but I didn't make any class (I think). What did I do wrong?

  • Thanks for the replies, it's very easy to miss that the C is supposed to be capitalized...

correct your method name.

str.toUpperCase();

Java is Case sensitive.

Java ain't VBA you know. The language is case-sensitive (like C and C++).

You need to write toUpperCase() instead.

public String endUp(String str) {
  int l = str.length();
  if (l < 4) {
    str = str.toUpperCase();
    return str;
  }else{
    String strup = str.substring(l-3);
    strup = strup.toUpperCase();
    return (str.substring(0,l-3) + strup);
  }
}

The correct syntax is toUpperCase() Refer to the API https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/String.html

你已经拼写像 toUppercase() 但方法的实际名称是toUpperCase()而下面的行有效
"myString".toUpperCase()

You have made a case-sensitive error. You have to do it like this str.toUpperCase()

public String endUp(String str) {
        int l = str.length();
        if (l < 4) {
            str = str.toUpperCase();
            return str;
        }else{
            String strup = str.substring(l-3);
            strup = strup.toUpperCase();
            return (str.substring(0,l-3) + strup);
        }
    }

Try like this.It should be .toUpperCase();

public class Ex {

    public String endUp(String str) {
        int l = str.length();

        if (l < 4) {
            str = str.toUpperCase();
            return str;
        }else{
            String strup = str.substring(l-3);
            strup = strup.toUpperCase();
            return (str.substring(0,l-3) + strup);
        }
    }

    public static void main(String args[]){

        Ex ex1=new Ex();
        System.out.println("output : " +ex1.endUp ("musni"));


    }

}

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