Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

java - Why am I "missing return statement" in one case, but not the other?

I am writing a code that will import a string of characters from a text file, analyze the string using a stack and determine which "language" the string belongs to based on whether or not it fits certain rules. Why is it in this case:

public static boolean checkL2(File file) throws IOException
{
    Stack l2Stack = new Stack();
    boolean bStart = false;
    char w;

    Scanner sc = new Scanner(file).useDelimiter("\s*");

    while(sc.hasNext()) //Load input from file onto stack
    {
        w = sc.next().charAt(0);

        if (w == 'A')
        {
            if (bStart == true)
            {
                return false;
            }
            else
            {
                l2Stack.push('A');
            }
        }
        if (w == 'B')
        {
            bStart = true;
            if (l2Stack.isEmpty() == true)
            {
                return false;
            }
            else
            {
                l2Stack.pop();
            }
        }
    }
    sc.close();
    if (l2Stack.isEmpty() == true)
    {
        return true;
    }

}

I get an error telling my that I am missing a return statement, but not in this one:

 public boolean isEmpty()
{
    if (top == -1) {
        return true;
    }
    else
    {
        return false;
    }
}

They look like very similar to me and I can't figure out why one works fine and the other does not. I would like to have the first bit of code return true or false based one whether the string fits the rules A^nB^n.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As already mentioned by the other answers, the reason why this is happening is because you declare your function as returning a boolean but it is possible for you to execute your function without actually hitting a return statement. For example imagine if Just to be a bit more explicit here is your code and where extra returns are needed:

    public static boolean checkL2(File file) throws IOException
    {
        Stack l2Stack = new Stack();
        boolean bStart = false;
        char w;

        Scanner sc = new Scanner(file).useDelimiter("\s*");

        while(sc.hasNext()) //Load input from file onto stack
        {
            w = sc.next().charAt(0);

            if (w == 'A')
            {
                if (bStart == true)
                {
                    return false;
                }
                else
                {
                    l2Stack.push('A');
                }
            }
            if (w == 'B')
            {
                bStart = true;
                if (l2Stack.isEmpty() == true)
                {
                    return false;
                }
                else
                {
                    l2Stack.pop();
                }
            }
        }
        sc.close();
        if (l2Stack.isEmpty() == true)
        {
            return true;
        }
//Added return 
    return false;
    }

This added return is necessary because imagine what would happen if when your while loop finishes, l2Stack.isEmpty() == false, in this case you would reach the end of a "non-void" function without returning anything which Java does not allow. There is no default return action, you must explicitly declare what you want to return in every case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...