Hi everyone, Java newbie here. I'm trying to compile this code, but I'm getting errors on lines 40 and 42 saying it can't find the symbols balance and name that I defined in the beginning of the code. I'm not quite sure what I did wrong, and would appreciate any help. Thanks!

1.    import java.io.*;
2.    import java.util.*;
3.    public class BankAccount
4.    {
5.      public BankAccount(double b, String n)
6.      {
7.          balance=b;
8.          name=n;
9.      }
10.     public double deposit(double d)
11.     {
12.         balance=balance+d;
13.         return balance;
14.         
15.     }
16.     public double withdraw(double w)
17.     {
18.         balance=balance-w;
19.         return balance;
20.     
21.     }
22.     
23.     
24.     }
25.     public double balance;
26.     public String name;
27.     
28.     class Tester
29.     {
30.         public static void main(String args[])
31.         {
32.             Scanner kbReader=new Scanner(System.in);
33.             System.out.print("Initial Deposit: ");
34.             double b=kbReader.nextInt();
35.             System.out.print("Account Name: ");
36.             String n=kbReader.nextLine();
37.     
38.             BankAccount myAccount=new BankAccount(b, n);
39.             myAccount.deposit(505.22);
40.             System.out.println(balance);
41.             myAccount.withdraw(100.00);
42.             System.out.print("The "+name+"account balance is, $"+balance);  
43.         }
44.     }

What class are balance and name defined in?

Please post code without the numbers at the start of every line. The numbers make it impossible to do a test compile.

 24.     }
 25.     public double balance;
 26.     public String name;

on line 24, you close the class BankAccount. then, you write two variables that are not in a class, and then you start your Tester class, which does not contain balance or name.
quite normal it can't find it.

My suggestion: remove the closing bracket on line 24,
remove the Tester class, delcare balance and name as private and put that main method within your BankAccount class, or use your getters instead of directly trying to access those variables, that's what they're there for.

This article has been dead for over six months. Start a new discussion instead.