Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First Time Java User

Status
Not open for further replies.

Caden

Programmer
Dec 9, 2001
101
CA
Greetings Tek-Tips, so, i'm a first time Java users, I downloaded 1.4.2 SDK and installed it, I also downloaded Jcreator to write my code in.

So, I have a little program that i'm trying to make work, and here it is

/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance
*/
public BankAccount()
{
balance = 0;
}

/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}

/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}

/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}

/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}

private double balance;
}


Now, it complies no problem, but when I try to execute it, a command window pops up (like it should) and it tells me this...

Exception in the thread "main" java.lang.NoSuchMethodError: main
Press any key to continue

And when a key is pressed, the window disapears.

So basically I can't see what happens with my program, and I have no idea how to fix it.

Any help would be terrific. Thanks
 
Like the message indicates, you do not have a main method. And that is the method that is being "called" when running the program. For example add the following method to your class :
Code:
  public static void main(String[] args) {
    BankAccount acc = new BankAccount(500);
    System.out.println("Account = " + acc.getBalance());
    acc.deposit(200);
    System.out.println("Account = " + acc.getBalance());
    acc.withdraw(300);
    System.out.println("Account = " + acc.getBalance());
  }
=========
The output will be :
Account = 500.0
Account = 700.0
Account = 400.0
=========
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top