Results 1 to 3 of 3
  1. #1
    Rhyssa6 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Savings account balance comparison code.

    I have a savings account assignment which includes the following step:
    a public method of the appropriate type called equals, as per Java requirements, it will compare two accounts and return true if both
    savingsBalance values are the same - accurate to 4 digits after the decimal point (HINT: use strings).

    I've used the following code to convert the savings balance to a string:

    public String currentBalance()
    {
    String currentBalance = "" + four.format(savingsBalance);
    return currentBalance;
    }

    This is called from the main program by:
    String acctOne = acct1.currentBalance();
    String acctTwo = acct2.currentBalance()

    If I put this code:

    public boolean equals() //this is line 40
    {
    if (acctOne.equals(acctTwo))
    {
    System.out.println("The account balances are equal.");
    return true;
    }
    else
    {
    System.out.println("The account balances are not equal.");
    return false;
    }
    }

    in my main file then I get the following error messages on line 40:
    illegal start of expression
    ; expected.

    If I remove the word public then I don't get the illegal start message but I still get the ; expected one.

    If I put the boolean method into my program file (which is where I suspect it should be) then it can't find the values because they are calling objects in the main file.

    Can anyone tell me what I'm doing wrong please?

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    To solve this try making equals receive to parameters; the a String acctTwo in this way you use equals from the main method as following :
    boolean equ = acctTwo.equals(acctTwo);
    if (equ == true){
    System.out.println("The account balances are equal.");
    } else {
    System.out.println("The account balances are not equal.");
    }

    and the equals is as follow :
    public boolean equals(String compare){
    return this.equals(compare);
    }
    hoped i helped and understood your problem correctly
    Last edited by baf06; 04-10-2011 at 10:29 AM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Rhyssa6 View Post
    In my main file then I get the following error messages on line 40:
    illegal start of expression
    ; expected.

    If I remove the word public then I don't get the illegal start message but I still get the ; expected one.

    If I put the boolean method into my program file (which is where I suspect it should be) then it can't find the values because they are calling objects in the main file.

    Can anyone tell me what I'm doing wrong please?
    Those type of error messages indicate that you've put your equals( ... ) method in the wrong place or that your curly brackets aren't balanced. Make your file compile first before you start thinking any further.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  2. Changing balance variable so its never negative
    By coding in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 05:59 AM
  3. Savings Account Comparison
    By shivam kush in forum Networking
    Replies: 0
    Last Post: 10-19-2010, 10:03 AM
  4. Calculate the remaining balance on a loan
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 9
    Last Post: 03-30-2010, 03:38 AM
  5. Color Balance
    By THEAniKan in forum Java 2D
    Replies: 2
    Last Post: 09-14-2009, 04:34 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
  •