CodeHS上的Java调试

时间:2016-08-28 23:58:35

标签: java debugging exception

我需要帮助调试这个;问题出在第10-13行。它说我无法从String转换为Int,但我不知道如何解决它。代码查找单词" bug"在给定的字符串内。

        public class BugHunter extends ConsoleProgram
{
    public void run()
    {
        String test1 = "Debug";
        String test2 = "bugs bunny";
        String test3 = "boogie";
        String test4 = "baby buggie";
        int index1 = findBug(test1);
        int index2 = findBug(test2);
        int index3 = findBug(test3);
        int index4 = findBug(test4);

        printBug(test1, index1);
        printBug(test2, index2);
        printBug(test3, index3);
        printBug(test4, index4);
    }

    // Returns the index of the String "bug" inside the String str
    // If str does not contain the String "bug", returns -1
    public String findBug(String str)
    {
        str.indexOf("bug");
    }

    public void printBug(String test, int index)
    {
        if(index != -1)
        {
            System.out.println(test + " has a bug at index " + index);
        }
        else
        {
            System.out.println(test + " has no bugs");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

findBug似乎应该返回单词bug的索引int。所以它应该看起来像

public int findBug(String str)
{
    return str.indexOf("bug");
}