Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Is the Java Compiler retarded?

Name: Anonymous 2009-10-27 3:15

Its telling me I need a return statement on the last line of my method even though that part can never be executed.. I am a novice at Java and I am teaching myself.. so is it just me that has bad coding style or is the java compiler a bit too OCD?

Name: Anonymous 2009-10-27 20:16

Okay, >>16-chan, so here are the ways in which your anus enjoys extracting every last drop of my cum with a delicate squeeze of its folds. First, you're checking whether isTranslatable has been set to false, even though it will always be false once that loop exits. This introduces your actual problem: The Javac is smart enouch to notice that you neglected to handle the else condition of your if statement, but not smart enough to notice that the if statement is itself completely redundant.

Second, you're combining two forms of program logic anyway! >>18-kun wrote a nice version that does what you want, but here's a version that separates the logic in a way you might understand bettar.

public static String translate(String[] sourceLanguage, String[] destinationLanguage, String toBeTranslated) {
    int i = lookup(toBeTranslated, sourceLanguage);
    if (i < 0) { // ...then the string is not in sourceLanguage.
        return "?????";
    } else {
        return destinationLanguage[i];
    }
}

public static int lookup(String whatever, String[] inAnArray) {
    for (int i = 0; i < inAnArray.length; i++) {
        if (w.equals(whatever)) {
            return i;
        } else {
            // do nothing, let the loop continue
        }
    }
   
    // Wasn't found
    return -1;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List