Results 1 to 6 of 6
  1. #1
    qadeer37 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default giving missing return statement error.due to withdraw method.

    class Account
    {
    private String name;
    int account;
    int balance;

    public Account(String owner, int initial, int accNumber)
    {
    name = owner;
    account = accNumber;
    balance = initial;

    if (balance <= 10)
    System.out.println("Out of balance");
    }

    public int withdraw (int amount)
    {
    if (amount <= 0)
    System.out.println ("Transaction could not be done");

    else
    {
    balance -= amount;
    return balance;
    }
    }
    }





    public class Bank
    {
    public static void main(String args[])
    {
    Account acct = new Account("qadeer", 8, 345);



    }
    }

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

    Default

    Quote Originally Posted by qadeer37 View Post
    Java Code:
       public int withdraw (int amount)
       {	
    	if (amount <= 0)
    	System.out.println ("Transaction could not be done");
    	
    	else
    	{
    	   balance -= amount;
    	   return balance;
    	}
       }
    What will be returned if amount <= 0? See? That's why your compiler complained.

    kind regards,

    Jos

  3. #3
    qadeer37 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default Thanks it worked

    so a method must have a return type! right?

  4. #4
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    It must if you specify it has. You said it returns an int; well you've just given it a condition where it returns nothing.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, a method must have a return type. However, that return type can be void (i.e. it returns nothing). The point is, and what the compiler was complaining about, is that if you declare the method to return something, then that method must return it, regardless of the "path" taken within the method. I.E. if the method ends with an if statement and returns from within that if statement, then every block of that if statement must return something.

  6. #6
    bdtagar is offline Member
    Join Date
    Jun 2009
    Posts
    11
    Rep Power
    0

    Default

    Hi,
    I think this will help:

    class Account
    {
    private String name;
    private int account;
    private int balance;

    public Account(String owner, int initial, int accNumber)
    {
    name = owner;
    account = accNumber;
    balance = initial;
    }
    public void withdraw (int amount)
    {
    // set balance
    balance = 100;
    if (balance <= 10)
    System.out.println("Out of balance");
    else if (amount <= 0)
    System.out.println ("Transaction could not be done");
    else
    {
    balance -= amount;
    System.out.println(balance + " withdrawn!");
    }
    }

    }

    public class Bank
    {
    public static void main(String args[])
    {
    Account acct = new Account("qadeer", 8, 345);
    acct.withdraw(32);
    }
    }

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. Replies: 18
    Last Post: 12-02-2009, 12:01 PM
  3. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM
  4. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  5. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04: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
  •