Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By gimbal2

Thread: Import java,util.Scanner error

  1. #1
    rockets97 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Import java,util.Scanner error

    Hello all,

    I have been troubleshooting this program for hours, trying several configurations, and have still had no luck. Keep received error: class, interface, or enum expected. Please help.
    Thank you,

    Java Code:
    public class SavingsAccount
    {
       double balance;
       double annualInterestRate;
       double monthlyInterestRate;
       double totalDeposits;
       double totalWithdraws;
       double totalInterest;
       public SavingsAccount (double starting_Balance)
       {
          balance = starting_Balance;
       }
       public void setAnnualInterestRate (double annual_Interest_Rate)
       {
          setAnnualInterestRate = annual_Interest_Rate;
          monthlyInterestRate = annual_Interest_Rate /12;
       }
       public void deposit (double amount)
       {
          balance += amount;
          totalDeposits += amount;
       }
       public void withdraw (double amount)
       {
          balance -= amount;
          totalWithdraws += amount;
       }
       public void calculateMonthlyInterest ()
       {
          totalInterest = totalInterest + balance * monthlyInterestRate;
          balance = balance + balance * monthlyInterestRate;
       }
       public void displayData()
       {
          balance = Math.round (balance * 100.0) /100.0;
          totalInterest = Math.round (totalInterest * 100.0) /100.0;
          System.out.println();
          System.out.println("The ending balance         : $" + balance);
          System.out.println("Total amount of deposits   : $" + totalDeposits);
          System.out.println("Total amount of withdraws  : $" + totalWithdraws);
          System.out.println("Total interest earned      : $" + totalInterest);
        }
    }
    
    import java.util.Scanner;
    public class SavingsAccountTest
    {
      public static void main(String []args)
        {
          double staring_Balance;
          double annual_Interest_Rate;
          int months;
          double deposit_Amount;
          double withdraws_Amount;
          //Create an object for Scanner class
          Scanner input = new Scanner(System.in);
          //Prompt the user for starting balance
          System.out.print("Enter starting balance: $");
          staring_Balance = input.nextDouble();
          //Prompt the user for annual interest rate
          System.out.print("Enter annual interest rate: ");
          annual_interest_Rate = input.nextDouble();
          //Prompt the user for number of months
          System.out.print("Enter number of months: ");
          months = input.nextInt ();
          //Create an object for SavingsAccount class
          SavingsAccount sa = new
          SavingsAccount (starting_Balance);
          //Call to setAnnualInterestRate method
          sa.setAnnualInterestRate (annual_Interest_Rate);
          //Enter into the loop
          for (int i =0; i <months; i++)
          {
             //Prompt the user for the amount deposit
             System.out.print("Enter the amount to deposit for the month " + (i +1) + " : $");
             deposit_Amount = input.nextDouble ();
             //Call to deposit method
             sa.deposit (deposit_Amount);
             //Prompt the user for the amount to withdraws
             System.out.print("Enter the amount to withdraws for the month " + (i +1)+ " : $");
             withdraws_Amount = input.nextDouble ();
             //Call to withdraw method
             sa.withdraw(withdraw_Amount);
             //Call to calculateMonthlyInterest method
             sa.calculateMonthlyInterest ();
           }
           //Call to displayData method
           sa.displayData();
        }
      }
    Last edited by JosAH; 02-20-2014 at 05:10 PM. Reason: added [code] ... [/code] tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default Re: Import java,util.Scanner error

    When posting code, please use the code tags to preserve formatting.

    You have a syntax error on or around whatever line it's complaining about. What line is causing the error?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Import java,util.Scanner error

    @OP: I added those code tags for you; did you put both classes in one file? If so, don't do that; split it up in two separate files.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    rockets97 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Import java,util.Scanner error

    Thank you for adding to code tags; sorry, first time posting. Line 45 is causing a error: class, interface, or enum expected.

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

    Default Re: Import java,util.Scanner error

    Quote Originally Posted by rockets97 View Post
    Thank you for adding to code tags; sorry, first time posting. Line 45 is causing a error: class, interface, or enum expected.
    Did you read what I wrote?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,720
    Rep Power
    5

    Default Re: Import java,util.Scanner error

    Quote Originally Posted by JosAH View Post
    Did you read what I wrote?
    No I didn't. What's the name of the book?
    KevinWorkman and JosAH like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default Re: Import java,util.Scanner error

    Quote Originally Posted by gimbal2 View Post
    No I didn't. What's the name of the book?
    At first I thought this was OP and I thought "that little smartass..." and then I read the name and I thought "that little smartass..."
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    rockets97 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Import java,util.Scanner error

    Quote Originally Posted by JosAH View Post
    Did you read what I wrote?

    kind regards,

    Jos
    Thank you. "split it up in two separate files" did it.

Similar Threads

  1. Replies: 8
    Last Post: 10-16-2013, 10:26 PM
  2. Using import java.util.Scanner
    By The Taco in forum New To Java
    Replies: 2
    Last Post: 08-10-2013, 09:26 PM
  3. import java.util ????
    By kris09 in forum New To Java
    Replies: 3
    Last Post: 08-11-2008, 12:39 AM
  4. The import java.util.Scanner cannot be resolved
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 01:05 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
  •