【问题标题】:Java debugging on CodeHSCodeHS 上的 Java 调试
【发布时间】:2017-01-04 22:22:47
【问题描述】:

我需要帮助调试这个;问题在第 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");
        }
    }
}

【问题讨论】:

标签: java debugging exception


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多