简体   繁体   中英

Trying to compile code but keep getting the same error?

I keep getting the error: "missing return statement." Isn't my return statement listed 5 times? Does anyone know why I'm getting this and how to fix it? It refers to the second to last bracket. Any help/ideas as to why this is happening is appreciated. Thanks.

public class words
    {
        // instance variables - replace the example below with your own
        private String w;

        /**
         * Default Constructor for objects of class words
         */
        public words()
        {
            // initialise instance variables
            w="";
        }

        /**
         * Assignment constructor
         */
        public words(String assignment)
        {
            w=assignment;
        }

        /**
         * Copy constructor
         */
        public words(words two)
        {
            w=two.w;
        }

        /**
         * Pre: 0<=i<length( )
         * returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
         */
        private boolean isVowel(int i)
        {
            if (w.charAt(i)=='a')  
            return true; 
            else if (w.charAt(i)=='e')
            return true;
            else if (w.charAt(i)=='i')
            return true;
            else if (w.charAt(i)=='o')
            return true;
            else if (w.charAt(i)=='u')
            return true;
        }

    }

Tell me what do you return if w.charAt(i) is 'b'. You need to add a last line:

private boolean isVowel(int i)
        {
            if (w.charAt(i)=='a')  
            return true; 
            else if (w.charAt(i)=='e')
            return true;
            else if (w.charAt(i)=='i')
            return true;
            else if (w.charAt(i)=='o')
            return true;
            else if (w.charAt(i)=='u')
            return true;
            else return false;
        }
private boolean isVowel(int i){
   //...
   return false;
}

You are missing the case when your i is not a vowel.

Problems

  1. Use brackets. Code without them is annoying to read.
  2. Use the && operator when checking multiple if statements with the same body (all return true)
  3. Use a switch statement if you are comparing the same thing ( w.charAt(i) ) multiple times but they have different bodies
  4. The actual problem you have here is that if w.charAt(i) is not a vowel then it returns nothing. Include a return statement after all your checks
  5. Use a for loop with an array of vowels

(Note: I have intentionally not included code because it is not helpful to give you the answer. If you don't understand any of the terms used above, comment or google them to understand completely. That will allow you to get the most out of the answer.)

private boolean isVowel(int i)
    {
        if (w.charAt(i)=='a')  
        return true; 
        else if (w.charAt(i)=='e')
        return true;
        else if (w.charAt(i)=='i')
        return true;
        else if (w.charAt(i)=='o')
        return true;
        else if (w.charAt(i)=='u')
        return true;
       return false;//Default return statement if nothing has matched.
    }

--You are missing default return statement. If no match found what your metohd will return ? --It was the issue, i have updated your code here, if nothing found it will return false.

While other people are explaining the code does not compile because of a missing return statement, I would like to point out you can basically do this as an one liner as shown below.

private boolean isVowel(int i) {
    return w.charAt(i) == 'a' || w.charAt(i) == e || w.charAt(i) == 'i' || w.charAt(i) == 'o' || w.charAt(i) == 'u';
}

You are missing the return in your code. Ideally you should not have that many returns.

 private boolean isVowel(int i)
{
    boolean found=false;
    if (w.charAt(i)=='a')  
        found= true; 
    else if (w.charAt(i)=='e')
        found= true;
    else if (w.charAt(i)=='i')
        found= true;
    else if (w.charAt(i)=='o')
        found= true;
    else if (w.charAt(i)=='u')
        found= true;

    return found;
}

You have two options 1. to use a flag like above. you should use brackets, that makes the code easy to read. 2. at the end of your code, just add return false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM