尝试使用带有方法作为参数的子字符串,但不断出现错误

时间:2018-10-24 22:22:43

标签: java

我一直试图使用子字符串返回一行代码,但是每次尝试编译时,都会出现错误“找不到符号-方法findFirstVowel()”,该错误出现在代码的最后一行。为什么这不起作用? findFirstVowel应该返回一个整数吗?而且此方法也不需要输入-因此参数应为(0,即findFirstVowel的值)。有谁知道如何解决这一问题?谢谢!

public class words
{
    private String w;

    /**
     * Default Constructor for objects of class words
     */
    public words()
    {
        // initialise instance variables
        w="";
    }

    /**
     * Assignment constructor
     */
    public words(String assignment)
    {
        w=assignment;
    }

    /**
     * Copy constructor
     */
    public words(words two)
    {
        w=two.w;
    }

    /**
     * Pre: 0<=i<length( )
     * returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
     */
    public boolean isVowel(int i)
    {
        char vowel = w.charAt(i);
        return vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u';
    }

    /**
     * determines whether the first vowel in the String is at location 0, 1, 2, or 3 (don’t worry about exceptions)
     */
    public int findFirstVowel()
    {
        if (isVowel(0))
            return 0;
        else if (isVowel(1))
            return 1;
        else if (isVowel(2))
            return 2;
        else if (isVowel(3))
            return 3;
        else
            return -1;
    }

    /**
     * returns the Pig-Latin version of the String
     */
    public String pigify()
    {
        return w.substring(w.findFirstVowel()) + w.substring(0,w.findFirstVowel()) + "ay";

    }

1 个答案:

答案 0 :(得分:1)

findFirstVowel不是String类的一部分,而是您的类的一部分。因此不要在作为类String的实例的w上调用它

这是固定代码

public String pigify() {
    return w.substring(findFirstVowel()) + w.substring(0, findFirstVowel()) + "ay";
}