Results 1 to 4 of 4
  1. #1
    praveen is offline Member
    Join Date
    Oct 2015
    Posts
    2
    Rep Power
    0

    Default Can anyone help me in this programme..

    //program to find average of two numbers using return type.

    import java.lang.*;
    import java.util.*;
    class Average
    {
    avg(int x,int y)
    {
    float k=(float)(x+y)/2;
    return(k);
    }
    }
    class mainclass3
    {
    public static void main(String[] args)
    {
    Scanner s=new Scanner(System.in);
    System.out.println("enter value of a");
    int a=s.nextInt();
    System.out.println("enter value of b");
    int b=s.nextInt();
    Average s=new Average();
    float z=obj.avg(a,b);
    System.out.println("average"+z);
    }
    }

    output:
    mainclass3.java:5:error:invalid method declaration;return type required
    avg(int x, int y)
    Last edited by praveen; 10-13-2015 at 06:47 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Can anyone help me in this programme..

    Your avg( ... ) method doesn't have a return type; every method should have one (even 'void' will do); that's why your compiler is complaining ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    praveen is offline Member
    Join Date
    Oct 2015
    Posts
    2
    Rep Power
    0

    Default Re: Can anyone help me in this programme..

    Quote Originally Posted by JosAH View Post
    Your avg( ... ) method doesn't have a return type; every method should have one (even 'void' will do); that's why your compiler is complaining ...

    kind regards,

    Jos
    hey jos now what should i do this in program

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Can anyone help me in this programme..

    Jos already told you. Read more about methods here -> Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    And please place your code between code tags to format it. Read about them here -> BB Code List - Java Programming Forum - Learn Java Programming

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2012, 06:10 PM
  2. Need help with first Java programme.. Very new!
    By xxdave in forum New To Java
    Replies: 3
    Last Post: 11-22-2011, 04:46 PM
  3. Help with BinarytoDecimal programme
    By Thompson in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 10:43 PM
  4. cannot compile the programme
    By Roshini in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 11:02 AM

Posting Permissions

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