简体   繁体   中英

String method is undefined for type string?

When I try to compile/read Eclipse, I come up with the following error: "The method titleCase(String) is undefined for the type String"

Why is that?

Below is code:

public class Main {

    String titleCase(String s) {
        String k = s.substring(0, 1).toUpperCase()
                + s.substring(1).toLowerCase();
        return k;
    }

    public static void main(String args[]) {
        String name;
        do {
            System.out.println("Enter a new name");
            Scanner namescanner = new Scanner(System.in);
            name = namescanner.nextLine();
            String editednames = editednames.titleCase(name);

            ArrayList<String> names = new ArrayList<String>();
            names.add(editednames);
            System.out.println(names);
        } while (!(name.equalsIgnoreCase("Stop")));
    }
}

Replace this:

String editednames = editednames.titleCase(name);

with this:

String editednames = titleCase(name);

Also, you should declare the titleCase() method static so you can call it from inside the static main method:

static String titleCase(String s) {
    ...

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