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

问题描述

我一直在尝试使用子字符串返回一行代码,但每次我尝试编译时都会收到错误“找不到符号 - 方法 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";

    }

标签: java

解决方案


findFirstVowel不是 String 类的一部分,它是你的类的一部分。所以不要调用它w是 String 类的一个实例

这是固定代码

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

推荐阅读