Results 1 to 5 of 5
  1. #1
    ddonn is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Beginner needs help

    I am trying to create a new BankAccount object with an initial balance, the withdraw an amount from that object. I, then want to display the amount after the withdrawal using JOptionPane showMessageDialog method.
    I have 2 files, the BankAccount class file(attached) which I compiled and another file which calls methods from the BankAccount file.
    When I try to compile the second file I get an error saying "cannot find variable balance".
    I have tried declaring a variable balance but then I get an error saying "variable balance might not have been initialised".
    Can anyone point me in the right direction?? Any help would be much appriciated.
    Thanks in advance.

    import javax.swing.JOptionPane;
    public class FixedStartingBankBalanceAccount
    {
    public static void main(String[] args)
    {


    //Constructor; calls constructor method from BankAccount class
    BankAccount myFirstAccount = new BankAccount(5000);// Creates new BankAccount object with an initial balance

    //***Methods***

    //Calls withdraw method from BankAccount class
    myFirstAccount.withdraw(200);//balance = balance - amount

    //Calls getBalance method from BankAccount class
    myFirstAccount.getBalance();//Retrieves balance from myFirstAccoun object

    JOptionPane.showMessageDialog(null, "myFirstAccount contains:" + balance);
    }
    }
    Attached Files Attached Files

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

    Default Re: Beginner needs help

    Look at your last two lines of code (ignore empty lines and curly brackets for now): what is getBalance() doing? Is it returning the balance value of the account? Why aren't you storing (assigning) it somewhere? And, what is that (undeclared) variable 'balance' doing in your last line of code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ddonn is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: Beginner needs help

    OK I understand the problem there, but if i put something like, "double balance;" into my code I still get an error saying variable balance may not be initialised.
    I suppose where I am getting confused is when I call a method from the BankAccount class e.g myFirstAccount.withdraw(200); it goes to that class and does something like this, balance = balance - amount;
    Do I need to declare all variables e.g balance and amount and if so how do I do it because double balance;
    double amount; doesnt work.
    Kind regards.

  4. #4
    ddonn is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: Beginner needs help

    I think i need to declare instance fields, where the declaration requires an access specifier, type and name of variable e.g private double balance.

    I just don't know where to do this.

    Can anyone assist?

    Thanks in advance

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default Re: Beginner needs help

    Just put your getBalance() call right in your showMessageDialog() call. You don't need a variable, let alone an instance variable.

Similar Threads

  1. beginner
    By Eiolvit in forum New To Java
    Replies: 4
    Last Post: 07-10-2011, 06:32 PM
  2. Beginner
    By gozuhair in forum New To Java
    Replies: 12
    Last Post: 07-10-2011, 04:14 PM
  3. JNI beginner
    By M77 in forum Advanced Java
    Replies: 2
    Last Post: 04-04-2011, 01:53 AM
  4. Beginner needs help!
    By Polyy in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 06:12 AM
  5. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 09:14 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
  •