0

I need to write a short recursive program for a class that checks if a string - t is transformation of another string - s. It simply needs to check if every character in s is also in t. For ex:

"sddbs" is not a transformation of "sdb"

"sddbs" is a transformation of "sddb".

public static boolean isTrans (String s, String t) 
{
    if (t.indexOf(s.charAt(0)) != -1)
        return true;
    else 
        return false;
    return isTrans(s.substring(1), t);     
}

And still.. the code doesn't work as expected. "unreachable statement" in the last line of the code.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Sharon M.
  • 11
  • 1
  • 1
    Because it is… unreachable. There is no way you haven't return before this line. – rom1v Jan 27 '17 at 10:57
  • 1
    The compilation error says it all, what more do you need??? – barak manos Jan 27 '17 at 10:58
  • What you have (the if/else) is equivalent to `return t.indexOf(s.charAt(0)) != -1` –  Jan 27 '17 at 10:58
  • You question explains answer `unreachable statement` :) – fabfas Jan 27 '17 at 10:59
  • If should be obvious that `if (t.indexOf(s.charAt(0)) != -1) return true; else return false;` will always return, hence anything after it will be unreachable. It should also be obvious, that this is an obfuscated version of `return t.indexOf(s.charAt(0)) != -1;` – Holger Jan 27 '17 at 10:59
  • 1
    BTW, what exactly make "sddbs" is a transformation of "sddb" but not of "sdb"? – barak manos Jan 27 '17 at 11:00
  • 1
    I'm voting to close this question as off-topic because it is homework – Noel M Jan 27 '17 at 11:00
  • You should read [Why does Java have an "unreachable statement" compiler error?](http://stackoverflow.com/questions/3795585/why-does-java-have-an-unreachable-statement-compiler-error) if you don't understand why this happen – AxelH Jan 27 '17 at 11:04

4 Answers4

2

The reason is quite simple:

there is no possible way to execute this line:

return isTrans(s.substring(1), t); 

why?:

you have a return IF this condition (t.indexOf(s.charAt(0)) != -1) is met and another if not....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

It's because of the Law of Excluded Middle. You can treat return as exit function.

public static boolean isTrans (String s, String t) {
    if (condition)
        return true;
    else 
        return false;
    //the condition must have been true or false so the method has already exited.
    return isTrans(s.substring(1), t);     
}

If the condition is true, you return true, if it's false you return false. Otherwise you call recursively. There is no otherwise.

xenteros
  • 15,586
  • 12
  • 56
  • 91
0

You final return statement is unreachable because your method body contains return statement for both if and else condition, hence assuring that the last return statement will never be reached.

But besides this I don't understand why you need to write a recursive function, though a non-recursive function using similar method calls will do the same thing:

public static boolean isTrans (String s, String t) 
{
    if (t.indexOf(s) > -1)
        return true;
    else 
        return false;    
}

Edit: As suggested by @Holger you can avoid unnecessary if else and replace your code with:

public static boolean isTrans (String s, String t) 
{
    return (t.indexOf(s) > -1) //returns true or false just like your if else

}

or even shorter:

public static boolean isTrans (String s, String t) 
{
    return t.contains(s); //internally contains() calls indexOf(), similar to what you are doing
}
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • …and why are you still writing `if(condition) return true; else return false;` instead of just `return condition;`? – Holger Jan 27 '17 at 11:11
  • Yes you are right, but just wanted to make my program seem similar to that of OP since he/she seems a beginner. I could have just written `return t.contains(s);` for most simple solution. – Mustafa sabir Jan 27 '17 at 11:14
  • That’s exactly why you should point the OP to the nonsense of such an `if` statement, before it becomes a habit. – Holger Jan 27 '17 at 11:16
0

According to your recursion method once you enter the if condition it would either return a true or false output. so your code never reaches the recursion statement. I would like to suggest my way of implementing your transformation program using recursion.

import java.util.Scanner;

public class Recursion {

    static int flag;

    public void isTransformation(String str1, String str2) {

        flag = str2.length();
        char ch1[], ch2[];
        ch1 = str1.toCharArray();
        ch2 = str2.toCharArray();

        if (ch1[0] == ch2[0]) {
            flag--;
            if (flag == 0) {
                System.out.println("Transformation");
                return;
            }
            isTransformation(str1.substring(1), str2.substring(1));
        } else
            System.out.println("Not a Transformation");
    }

    public static void main(String args[]) {
        String str1, str2;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter string 1: ");
        str1 = sc.nextLine();
        System.out.print("Enter string 2: ");
        str2 = sc.nextLine();
        Recursion r = new Recursion();
        if (str1.length() >= str2.length())
            r.isTransformation(str1, str2);
        else
            r.isTransformation(str2, str1);
        sc.close();
    }

}