DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2004
    Posts
    3

    Trouble with Input

    Im new to Java and am having a little trouble. Ive got a program that displays soem accoutn details and an interest rate. The problem is that I want to make it so the user can enter the account details, but i get an ';'expected error. Teh code is here:

    import static java.lang.System.out;

    class Account {
    String name;
    String address;
    double balance;

    void display(){
    out.print(name);
    out.print(" (");
    out.print(address);
    out.print(") has £");
    out.print(balance);
    }

    double getInterest(double percentageRate){
    return balance*percentageRate/100.00;

    }
    }
    import static java.lang.System.*;
    import java.util.Scanner;

    class UseAccount {

    public static void main(String args[]){

    Scanner myScanner=new Scanner(System.in);

    Account myAccount=new Account();
    Account yourAccount=new Account();

    String myAccount.name=myScanner.next();
    myAccount.address="222 Cybe Space Lane";
    myAccount.balance=24.03;

    yourAccount.name="Andy Booth";
    yourAccount.address="19 Moor Drive";
    yourAccount.balance=55.63;

    myAccount.display();

    out.print(" plus £");
    out.print(myAccount.getInterest(5.00));
    out.println(" interest ");

    yourAccount.display();
    double yourInterestRate=7.00;
    out.print(" plus £");
    double yourInterestAmount=yourAccount.getInterest(yourInterestRate);
    out.print(yourInterestAmount);
    out.println(" interest ");

    }
    }

    The problem line is in bold.

    Any help much appreciated!

    Thanks Alot!

    Andy Booth

  2. #2
    Join Date
    Nov 2004
    Posts
    3
    Fixed it! Just had to remove the "String" before myAccount.name. Ive got another problem now though, when I enter a name, if I put a space in it only uses the first word, so no good for putting "Andy Booth" because it just takes it as "Andy". How can I solve this?

    Cheers

    Andy Booth

  3. #3
    Join Date
    Sep 2004
    Posts
    223
    use the nextLine() method of the Scanner class, this will read in the whole line, not just the first token
    A kram a day keeps the doctor......guessing

  4. #4
    Join Date
    Nov 2004
    Posts
    3
    Sorted! Thanks alot for your help!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links