Help with Java homework! Intro stuff

Zanzaj·9/30/2016, 5:00:35 PM·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

Thunderspam9/30/2016, 5:01:55 PM1 votes

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

Zyorhist9/30/2016, 5:49:02 PM1 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.

Verxint9/30/2016, 5:50:59 PM1 votes

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

GaleWinUnleashed9/30/2016, 5:54:26 PM1 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 Guy9/30/2016, 6:36:15 PM1 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.