user3015822 user3015822 - 3 months ago 21
Java Question

Java debugging on CodeHS

I need help debugging this; the problems are on lines 10-13. It says I can't convert from a String to an Int, but i don't know how to fix it. The code looks for the word "bug" within the given strings.

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");
}
}
}

Answer

It appears that findBug is supposed to return the index of the word bug which is an int. So it should look something like

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