Results 1 to 18 of 18
  1. #1
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default there is no return statement

    an error tell me that there is no return statement .
    I don't know the cause

    Java Code:
    public class FirstTest
    {
    public static void main()
    {
    int one = 0;
    int two = 2;
    add(a, b);
    
    }
    public static int add(int one, int two)
    {
    int c = a + b;
    }
    }
    what's the problem?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public static int add(int one, int two)
    {
        int c = a + b;
        return c;
    }
    Every method in java must have a return type in its method signature. The add method has a return type of int which means that the method must return an int. i can call the add method with two arguemnts and get the sum of them back, it will return the sum to me: int x = add(5, 6);
    The type returned from the method must match the return type declared in the method signature.

  3. #3
    Deepa is offline Member
    Join Date
    Nov 2008
    Posts
    85
    Rep Power
    0

    Default Re:

    Hi,
    In this method,the Datatype is int.If we use method with datatype means ,we must return any value except void datatype.

    public static int add(int one, int two)
    {
    int c = a + b;
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Deepa View Post
    Hi,
    In this method,the Datatype is int.If we use method with datatype means ,we must return any value except void datatype.

    public static int add(int one, int two)
    {
    int c = a + b;
    }
    I think you should use correct word when you answering. If you really don't know that better to explain it well. Because newbies can confused on that.

    In this method return type is int. We cannot return any value, must be return the type define in the method definition.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Wrong example...

    Deepa ... that is a wrong example:
    Java Code:
    public static int add(int one, int two)
    {
    int c = a + b;
    }
    The above example does NOT return anything. it is lacking the return statement, which is the same problem/example that the OP had. Hardwire's example is correct because it shows how the method returns the requested int.

    gabriel... here's a link of the Sun tutorials that expalins a little bit on the return statement:
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Deepa is offline Member
    Join Date
    Nov 2008
    Posts
    85
    Rep Power
    0

    Default Re:

    it's not an eg.This is the method asked by gabriel

  7. #7
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    i'm a little bit confuse with the syntax.
    public static void main()
    {
    int one = 0;
    int two = 2;
    add(a, b);
    }

    in this case,a and b is undeclare, right?i think the coding should be
    public static void main()
    {
    int a = 0;
    int b = 2;
    add(a, b);
    }

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default you are correct

    angelicsign... hahaha ... you are correct... we all went looking for the missing return statement. The same also goes for the add () method:
    Wrong:
    Java Code:
    public static int add(int one, int two)
    {
    int c = a + b;
    }
    Right:
    Java Code:
    public static int add(int [B][COLOR="Red"]a[/COLOR][/B], int [B][COLOR="red"]b[/COLOR][/B])
    {
    int c = a + b;
    return c;
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    if modify his code,i'll do like that.
    not want to challenge or what,
    just discuss only.
    Java Code:
    public class FirstTest
    {
    public static void main([COLOR="red"]String [] args[/COLOR])//red highlight,this is a must for main
    {
    //declare int a,b and initialize value to them.
    int [COLOR="Red"]a[/COLOR] = 0;
    int [COLOR="Red"]b[/COLOR] = 2;
    System.out.println(add(a, b));
    //for output to the screen,as new user of java,may output to the prompt.
    }
    public static int add(int one, int two) //return datatype is int
    {
    //because he declare one,two on the top,so we just follow his declaration
    int c = [COLOR="Red"]one[/COLOR] + [COLOR="Red"]two[/COLOR];
    return c; //return datatype is int,so must return an int value
    }
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    So what you want to discuss about this.

    In my view, all your comments are fine, but need to have few clarification.

    //red highlight,this is a must for main
    Why you think it is must? What's the effect on un-highlighted code?

    //for output to the screen,as new user of java,may output to the prompt.
    What is the difference between prompt and screen? I think you are talking about the command prompt. Actually println() print the output to the command prompt, or the console. Actually the word 'console' refer in IDEs. Most IDE print those output to on a separate window itself.

    May be you are refer 'screen' for UI, is it? On UI you cannot add text using println(). You have to added different controls depend on your implementation and set text there.

  11. #11
    mazhathully is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    If we want to call the main method externally without creating an instance of the Class, the
    method signature must be as follows :
    public static void main(String[] args)

    Thats the reason why prevous code highlighted that portion.

    _Sujith

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you clarify with a simple example? I'm not clear what you are trying to say. As far as I know main method call by the VM.

  13. #13
    mazhathully is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    yea, correct.
    VM calls the main method, having the Signature previously mentioned.

    Can VM call main, with some other signature?
    If yes, how.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No you cannot. Main method is the entry point to any Java application. Only argument you can pass is a String array.

  15. #15
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    So what you want to discuss about this.

    In my view, all your comments are fine, but need to have few clarification.



    Why you think it is must? What's the effect on un-highlighted code?



    What is the difference between prompt and screen? I think you are talking about the command prompt. Actually println() print the output to the command prompt, or the console. Actually the word 'console' refer in IDEs. Most IDE print those output to on a separate window itself.

    May be you are refer 'screen' for UI, is it? On UI you cannot add text using println(). You have to added different controls depend on your implementation and set text there.
    First of all, i'm quite new in Java. so if anything wrong,just refine me k?
    This is what i mean discussion.mod,you really correct my wrong mind set,
    i thought for all main method in Java
    Java Code:
    public static void main([COLOR="Red"]String [] args[/COLOR])
    this code is a must,because currently i'm using textpad to write my code
    where my lecturer ask us to.
    when the highlighted code is missing,
    it will prompt an error like this
    Exception in thread "main" java.lang.NoSuchMethodError: main
    but actually not a must for other editor/IDE??:confused:

    Ya,and mod is right,new user should use prompt or console,and not screen for UI(user interface).THANKS mod. :p

    but for new user of java, i suggest not to use IDE such netbeans or eclipse,
    this is because normally IDE will auto generate the class for user,
    it will cause user lack of basic practice.
    I mean when the user start with a new blank page,he will not know how to start.:confused::(

    We are inside the New To Java Thread,so i think all of us should make our basic strong enough 1st before we go on to advance java.;)
    Last edited by angelicsign; 12-03-2008 at 01:29 PM.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Samething...

    You both are talking about the same thing...
    • If a class is going to have a "main" method (which makes it callable from the VM) it must have a String array as an argument, otherwise you get a nastygram like:

    Exception in thread "main" java.lang.NoSuchMethodError: main
    • prompt, command prompt & screen all refer to the old reliable "DOS Command Prompt" that Windows offers us.

    • I probably already knew it, but I would have to agree with Eranga that console refers to the IDE GUI. This way there is a way of differenciating between a Java program run from an IDE and one run from the DOS command prompt.


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed with you, because I've never advice to work on with any IDE at the start. Best choice is use Command Prompt + Notepad.

  18. #18
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    mod,sorry to do that,
    but really need your help cause i'm not enough post to private message you.
    that is my post on JavaServer Pages (JSP) and JSTL
    is about "how to receive uploaded image's data"?

Similar Threads

  1. import statement.
    By diRisig in forum New To Java
    Replies: 2
    Last Post: 02-08-2008, 12:34 AM
  2. Using Prepared Statement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:22 AM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •