I just needed some help with a code error. When I compile the code below, I keep getting this error:

Program2.java:12: error: ';' expected
Scanner keyboard = new Scanner(System.in);

1 error

Could somebody please let me know what I am doing wrong?


import java.util.Scanner;       // Needed for Scanner class
import java.text.DecimalFormat; // Needed for decimal place amounts

public class Program2
public static void main(String[] args)
BankAccount account;            // To reference a BankAccount object
double balance,                 // The acounts's starting balance interest rate

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat ("#0.00");

// Get the starting balance. 
System.out.print("What is your account's " + "starting balance? "); 
balance = keyboard.nextDouble(); 

// Get the Annual interest rate. 
System.out.print("What is your annual interest rate? "); 
interestRate = keyboard.nextDouble(); 

// Create a BankAccount object. 
account = new BankAccount(balance, interestRate); 

// Get the amount of pay for the month. 
System.out.print("How much were you paid this month? "); 
pay = keyboard.nextDouble(); 

// Deposit the user's pay into the account. 
System.out.println("We will deposit your pay " + "into your account."); 
System.out.println("Your current balance is $" + formatter.format( account.getBalance() ));

// Withdraw some cash from the account. 
System.out.print("How much would you like " + "to withdraw? "); 
cashNeeded = keyboard.nextDouble(); 

// Add the monthly interest to the account. 

// Display the interest earned and the balance. 
System.out.println("This month you have earned $" + formatter.format( account.getInterest() ) + " in interest.");
System.out.println("Now your balance is $" + formatter.format( account.getBalance() ) ); 
4 Years
Discussion Span
Last Post by poojavb

It is amazing that one character can mess the whole script up. I tried your suggestion but now it gives me a whole slew of errors instead of just the original error. I think I have a lot more problems with my script than I originally thought. Anymore suggestions or help would be appreciated. Thanks.


well, if you let us know what errors you get now, we might be able to help easier

interestRate = keyboard.nextDouble();

I assume the above line gives you problems? you have to declare variables before using them.


When I tried ur code...it shows error that few of ur variables are not initialized....Please check the initialization of variables,,,

Since I dont have ur BankAccount class I get the following errors

varaibles cannot be resolved to a variable for the below variables


This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.