user2963042
user2963042

Reputation: 15

Why won't it print anything?

This is the code I have written. But the code does not print anything even though it is compiling. I have also tried to include System.out.print statements under the if and else statements. What am I supposed to do in order to have it actually print something.

public class Numfive {
    public static void main(String[] args) {
        isReverse("hello", "eLLoH");
    }

    public static boolean isReverse(String s1, String s2) {
        if (s1.length() == 0 && s2.length() == 0) { 
            return true;
        } else if (s1.length() == 0 || s2.length() == 0) {
            return false;  // not same length
        } else {
            String s1first = s1.substring(0, 1);
            String s2last = s2.substring(s2.length() - 1);
            return s1first.equalsIgnoreCase(s2last) &&
            isReverse(s1.substring(1), s2.substring(0, s2.length() - 1));
        }
    }
}

Upvotes: 0

Views: 168

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34146

Because you don't have any print statement (that prints the result).

System.out.println(isReverse("hello", "eLLoH"));

Note:

I have also tried to include System.out.print statements under the if and else statements.

If you put a print statement after the if-else if-else structure, the program won't never reach it, because each block has a return.

Upvotes: 4

Related Questions