Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Missing Return Statement in If-elseIf-else

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Missing Return Statement in If-elseIf-else

    this is the code that has error ..

    "missing return statement"

        public static boolean ToDecimalCont() throws IOException {
     
            String keyIn;
     
            System.out.println("");
            System.out.print("Do You Want To Change The Numerical Type?: ");
            keyIn = br.readLine();
     
            if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
     
                return true;
            }
            else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {
     
                System.exit(0);
                //but when i add return false or true; it compiles fine.
            }
            else {
     
                return false;
            }
     
        }
    it WAS like this , and it doesnt have any errors such like that
        public static boolean ToDecimalCont() throws IOException {
     
            String keyIn;
     
            System.out.println("");
            System.out.print("Do You Want To Change The Numerical Type?: ");
            keyIn = br.readLine();
     
            if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
     
                return true;
            }
            else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
     
        }
    i've notice that IN THE FIRST CODE when I added another elseIf statement (the third one with System.exit(0)), the compiler is asking for a return statement... WHICH I ALREADY HAVE on the default part which is 'else',

    and regarding with the FIRST CODE' ,the THE THIRD BRANCH OF IF (the elseIf that has a System.exit(0);, when i add this after the System.exit(0);
     return false or true;
    , it compiles prefectly..

    questions:
    1.) why is the compiler asks me to define a return value? (when i already have the default or the else block)
    2.) is this somewhat kind of unreachable statement?

    need some clarification..
    Last edited by chronoz13; October 12th, 2009 at 07:44 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Missing Return Statement in If-elseIf-else

    Its because even though you have a return in the else block the compiler knows that if the code hits the else if statement with no return in it, the method wont return anything and it will complain about the missing return statement.

    If you remove your else block and just return false at the bottom you will solve this.

    public static boolean ToDecimalCont() throws IOException {
            String keyIn;
     
            System.out.println("");
            System.out.print("Do You Want To Change The Numerical Type?: ");
            keyIn = br.readLine();
     
            if ((keyIn.equalsIgnoreCase("Y")) || (keyIn.equalsIgnoreCase("YES"))) {
                return true;
            }
            else if ((keyIn.equalsIgnoreCase("N")) || (keyIn.equalsIgnoreCase("NO"))) {
                return false;
            }
            else if ((keyIn.equalsIgnoreCase("exit")) || (keyIn.equalsIgnoreCase("quit"))) {
                System.exit(0);
            }
     
            return false; // This should work for you because even if the last else if is hit, the compiler knows that there is a fail safe at the end which will return false.
        }

    // Json

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Missing Return Statement in If-elseIf-else

    oh damn.. what simple logic i missed, yah ofcourse just think of what will happen if that part of block became true
    it wont return anything....


    tnx sir Json!!

    solved!:

  4. The Following User Says Thank You to chronoz13 For This Useful Post:

    Json (October 12th, 2009)

Similar Threads

  1. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 10:17 AM
  2. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 01:00 PM
  3. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 08:49 PM
  4. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 01:33 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM