Help with Java homework! Intro stuff

Zanzaj··1 votes·960 views

public class Account {
public static void main(String[] args) { private double balance; <----- I keep getting an error here saying "Illegal start of expression" doesn't matter what line i put here

    //set balanace to $0.00
    public Account()
        {
            balance = 0;
        }
    
    //Credit
    public void credit (double amount)
        {
            balance = balance+amount;
        }
    
    //Withdraw
    public void withdraw (double amount)
        {
            balance = balance - amount;
        }
    
    //Balance inquiry 
    public double getBalance()
        {
            return balance;
        }
}

}_<---- and an error here "class, interface, or enum expected" _

9 Comments

Thunderspam1 votes

Rip, no idea i saw java and thought it was coffee

Zyorhist1 votes

do you have a command for balance defined, as far as I can tell you don't have it defined in the script so it wont run it. you do have getBalance but it's not the same. its been a while but you may also need () at the end of balance.

Verxint1 votes

Google is the friend of any programming student http://stackoverflow.com/questions/24562950/java-error-illegal-start-of-expression Always Google your errors.

GaleWinUnleashed1 votes

It's been way too long since I've done programming... Have you tried initializing "balance" when you declare it? It doesn't matter what you set it at since the first call to the class will set it to zero anyway.

Edit: Nevermind. Ditch the "private" in that line and it should be fine. That or declare it in the class, not in main (why the heck is this main, anyway? the whole program does absolutely nothing unless there's more to it)

Ph0ne Guy1 votes

All of your instance variables, constructors, and methods should be outside of the main method. The main method is where you actually use the other methods/constructors with the object.

Zanzaj1 votes

When i move private double balance

even outside the main string the error "illegal start of expression" just moves to the next line within the main string.

Ph0ne Guy1 votes

I believe you have to put everything outside of the main method. Right now all you have are instance variables and methods, which always go outside of it. You currently don't have anything for the program to actually run. So first, move everything out of the main method. Then, initialize a new account inside of the main method, which is also where you would call your methods. So basically, in the main method it would be something like:

Account a1 = new Account(); System.out.println(a1.balance); a1.credit(200); System.out.println(a1.balance); a1.withdraw(100); System.out.println(a1.balance);

Zanzaj1 votes

im trying to build a custom class to call on in another program. so my other program will call this method and perform the functions needed

Zanzaj1 votes

im trying to build a custom class to call on in another program. so my other program will call this method and perform the functions needed. package accounttest;

/**

*/ public class Accounttest {

/**

 */
public static void main(String[] args) 
{
    // create my account object
    Account a1 = new Account();
    a1.deposit(200);
    // create my second account object
    Account a2 = new Account();
    a2.deposit(100);
    
    




}

} This is my other code trying to call the class Account

More from [ARCHIVED] Miscellaneous