hi
im trying to check if two strings are equal but ignoring the vowels so that "hello" can equal "hallo" or even "hll". i have a recursive defination for looking at the vowels but i cant get it to work for ignoring the vowels. at the moment i get an error message saying there is no return statement but all my "if"s have "else"s to ensure a return statement

class equalsCalc
{
    boolean equals( String strA, String strB )
    {
        if       ( strA.length() == 0 && strB.length() == 0 )
        return true;
      
        else if  ( strA.length() == 0 && strB.length() != 0 )
        return false;
      
        else if  ( strA.length() != 0 && strB.length() == 0 )
        return false;
       
        else if  ( strA.charAt(0) != strB.charAt(0) )
        {
            char [] vowels = new char [5];
            vowels[0] = 'a';
            vowels[1] = 'e';
            vowels[2] = 'i';
            vowels[3] = 'o';
            vowels[4] = 'u';
            for(int j = 0; j<vowels.length; j++)
            {
                if (strA.charAt(0)!= vowels[j])
                    return false;
                else if (strB.charAt(0) != vowels[j])
                    return false;
                else
                    return equals(strA.substring(1), strB.substring(1));
            }
        }
      
        else
        return equals( strA.substring(1), strB.substring(1) );
  }
}

public class equalsTester
{
    public static void main(String[]args)
    {
        equalsCalc e = new equalsCalc();
        boolean result = e.equals("hello","hallo");
        System.out.println(result);
    }
}

return in looping constructs is considered conditional and hence the given compiler error. The compiler acts pretty much as a pessimist here and assumes the loop will never be entered unless the condition includes only literals.

// Won't work
public int doIt() {
  final int min = 0;
  final int max = 10;
  int i = min;
  while(i < max) {
    // something
    return something;
  }
}

// But this will
public int doIt() {
  while(0 < 10) {
    // something
    return something;
  }
}

To get around this, keep a boolean flag which would be updated when a condition is true inside the loop and break out of the loop. Check the boolean flag outside the loop and process accordingly.

I have saw your code. There is problem with it in here, else if (strA.charAt(0) != strB.charAt(0)) , you have a for looping sentence, because your looping sentence may be not excuted, so you can add a sentence behind the looping(return false), or behind the end of the method.
And I am willing to give you a advice,

else if (strA.length() == 0 && strB.length() != 0)
return false;
else if (strA.length() != 0 && strB.length() == 0)
return false;

you can use one sentence if(strA.length() == 0 || strB.length() == 0) return false.
It can get the same result.
And at last, I am sure that your code can't get the result you wanted. Because you have lost your mind.

It's a simple class. I would like give you my code. I have test, it did work. I hope it will help you.

public class EqualsCalc {
private static char[] vowels = {'a','e','i','o','u'};
private static String filterStr(String str){
if(str == null){
return null;
}else{
char[] temp = new char[str.length()];
int j = 0;
for(int i = 0; i < str.length(); i ++){
if(new String(vowels).indexOf(str.charAt(i)) < 0){
temp[j] = str.charAt(i);
j++;
}
}
return new String(temp).trim();
}
}
public static boolean equals(String strA, String strB) {
if(strA == null || strB == null){
return false;
}
else if (strA.length() == 0 && strB.length() == 0){
return true;
}
else if (strA.length() == 0 || strB.length() == 0){
return false;
}
if( filterStr(strA).compareTo(filterStr(strB)) == 0){
return true;
}else{
return false;
}
}
public static void main(String[]args)
{
boolean result = EqualsCalc.equals("hello","hallo");
System.out.println(result);
}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.