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 8 of 8

Thread: Error of "Class has no return statement"

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Error of "Class has no return statement"

    i can't figure out way the compiler say's the class has no return statement .....

    import java.io.*;
     
    class ConsoleInput { // open class
     
        public static String readLine() { // open readLine
            try { // open try
                StringBuffer line = new StringBuffer();
                int a = 0;
                char b;
                System.out.println("input here:");
                BufferedInputStream buff = new BufferedInputStream(System.in);
     
                while (a != -1) { // open while
                    a = buff.read();
                    b = (char) a;
                    line.append(b);
                } // close while
                String sLine = line.toString();
                if (sLine.length() == 0) { // open if
                    sLine = "no string";
                } // close if
                buff.close();
                return sLine;
            } catch (IOException e) { // open catch close try
                System.out.println(e.getMessage());
            } // close catch
        } // close readLine
     
    } // close class
    Last edited by Deep_4; November 8th, 2012 at 01:24 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: way missing return statement???

    Hello mdstrauss,

    The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method.
    Whenever your method is a variable type like String, the method must always return a String result.

    This is untested but for example:

    import java.io.*;
     
    class ConsoleInput { // open class
     
        public static String readLine() { // open readLine
     
            try { // open try
                StringBuffer line = new StringBuffer();
                int a = 0;
                char b;
                System.out.println("input here:");
                BufferedInputStream buff = new BufferedInputStream(System.in);
     
                while (a != -1) { // open while
                    a = buff.read();
                    b = (char) a;
                    line.append(b);
                } // close while
                String sLine = line.toString();
                if (sLine.length() == 0) { // open if
                    sLine = "no string";
                } // close if
                buff.close();
                return sLine;
            } catch (IOException e) { // open catch close try
                System.out.println(e.getMessage());
            } // close catch
            [B]return readLine();[/B]
        } // close readLine
     
    } // close class
    Or you can always make the return method void:

    import java.io.*;
     
    class ConsoleInput { // open class
     
        public static [B]void [/B]readLine() { // open readLine
     
            try { // open try
                StringBuffer line = new StringBuffer();
                int a = 0;
                char b;
                System.out.println("input here:");
                BufferedInputStream buff = new BufferedInputStream(System.in);
     
                while (a != -1) { // open while
                    a = buff.read();
                    b = (char) a;
                    line.append(b);
                } // close while
                String sLine = line.toString();
                if (sLine.length() == 0) { // open if
                    sLine = "no string";
                } // close if
                buff.close();
                //return sLine;
            } catch (IOException e) { // open catch close try
                System.out.println(e.getMessage());
            } // close catch
     
        } // close readLine
     
    } // close class
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    mdstrauss (August 2nd, 2009)

  4. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: way missing return statement??? qestion on anser

    thank you it works but i don't understand in method readLine i have all ready a return statement that returns string

    return sLine;

    way do i need the second return statement

    return readLine();

    in the readLine method ???

  5. #4
    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: way missing return statement???

    In the case where the first return statement won't be reached like for instance in an if you still need to have a return.

    Hope that makes sense.

    // Json

  6. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (August 2nd, 2009)

  7. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: way missing return statement???

    i don't get it way do i need two of the returns one before the catch and one after
    way not put only one after the catch and way the second one is "return readLine()"
    and not "return sLine" ?


    i don't get way the catch block makes a deference ??


    and i don't see where in my program there could be a hole with no return the if is to insure the is a return
    no matter what....?????



    thank you very much for the help
    i am knew to java .....

  8. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: way missing return statement???

    ok i understood that if my program catch's a exception then there would be no return but way dose the program no recognize my sLine string it is still in the method ?????


    i checked all these and they compile

     
    import java.io.*;
     
     
    class ConsoleInput
    {
     
     
     
    public static String readLine()
      { 
      try{
    StringBuffer line = new StringBuffer();
    int a = 0 ;
    char b;
    System.out.println("input here:");
    BufferedInputStream buff = new BufferedInputStream(System.in);
     
     
        while (a != -1 )
            {
              a = buff.read();
              b = (char) a;
              line.append(b);
            }
    string sLine = line.toString();
     
    buff.close();
    return sLine;
      }catch (IOException e){
                      System.out.println(e.getMessage());
                        [B]return null;[/B]
                                 } 
     
          }   
     
     
     
     
    }






    or this
     
    import java.io.*;
     
     
    class ConsoleInput
    {
    [B]static String sLine;[/B]
     
     
    public static String readLine()
      { 
      try{
    StringBuffer line = new StringBuffer();
    int a = 0 ;
    char b;
    System.out.println("input here:");
    BufferedInputStream buff = new BufferedInputStream(System.in);
     
     
        while (a != -1 )
            {
              a = buff.read();
              b = (char) a;
              line.append(b);
            }
    sLine = line.toString();
    if (sLine.length()==0){ 
    sLine = "no string";
    }   
    buff.close();
    return sLine;
      }catch (IOException e){
                      System.out.println(e.getMessage());
     
                                 } 
                [B]return sLine;[/B]
          }   
     
     
     
     
    }

  9. #7
    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: way missing return statement???

    Well basically when you have a return type on a method you must make sure the method returns something unless it throws an exception so even if you have a try catch in there you have to make sure you return something if you catch an exception. You could of course put your return after the catch which will be alright.

    There is a minor flaw in your code though, I see you are using a BufferedInputStream and you call close() in the try, but if an exception is thrown while you do buff.read() you will never reach the buff.close() and hence you will get a memory leak in your program every time you hit this method.

    Try something like this (with reservation for any errors as I type this from the top of my head):

        BufferedInputStream buff = null;
     
        try {
            buff = new BufferedInputStream(System.in);
            // Do some stuff with the buffer here
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Close the buffer here, because this will always
            if(buff != null) {
                try {
                    buff.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    // Json

  10. The Following User Says Thank You to Json For This Useful Post:

    mdstrauss (August 2nd, 2009)

  11. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: way missing return statement???

    thank you very very much your write it dose not work it keeps open a strem even if my input is enter say what is a memory leak and well it happen no matter if i get a exception i'll check your advice latter because i have to go

    again thank you very much

Similar Threads

  1. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM
  2. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  3. [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, 12:33 PM
  4. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM